2012-11-13 41 views
1

在我的代碼我已經加入這個變量剛開了jQuery的:不()選擇工作

var spellSpace = $('td[data-word=' + listOfWords[rndWord].name + ']').hasClass('.highlight-problem:not(.right-word)'); 

,並由於某種原因,它不會在此代碼

$('.next-question').click(function() { 
    $('td').removeClass('highlight-problem'); 
    var r = rndWord; 
    while (r == rndWord) { 
     rndWord = Math.floor(Math.random() * (listOfWords.length)); 
    } 
    $('td[data-word="' + listOfWords[rndWord].name + '"]').addClass('highlight-problem'); 
    $('td[data-word=' + word + ']').removeClass('wrong-letter').removeClass('wrong-word').removeClass('right-letter'); 
    var spellSpace = $('td[data-word=' + listOfWords[rndWord].name + ']').hasClass('.highlight-problem:not(.right-word)'); 
    if (spellSpace) { 
     addMedia(); 
    } 
}); 

它只是將工作不返回addMedia()函數,我不知道爲什麼

任何人都可以告訴我我要去哪裏錯了嗎?

+0

只是改變'hasClass'到'is'。 – caarlos0

回答

3

hasClass獲取類名作爲參數,而不是選擇器。您可以使用is()代替:

var spellSpace = $('td[data-word="' + listOfWords[rndWord].name + '"]') 
    .is('.highlight-problem:not(.right-word)'); 

或者,也許以下幾點:

var spellSpace = $('td[data-word="' + listOfWords[rndWord].name 
    + '"].highlight-problem:not(.right-word)').length > 0; 
2

或者,你可以嘗試使用jQuery not()過濾器。

var spellSpace = $('td[data-word=' + listOfWords[rndWord].name + ']').hasClass('highlight-problem').not('.right-word');