2012-11-01 57 views
0

在我的節目,我都稱爲類「正確的答案,我想在TD類,但我有它真正的麻煩。配售類

這是我加上‘正確的字’類 「spellWord」,但我如何將它添加到 「TD」 類代替。

var spellWord = $('.highlight-problem .drop-box'); 
      if (!spellWord.filter(':not(.occupied)').length) { 
       var wordIsCorrect = 0; 
       spellWord.each(function() { 
        if ($(this).parents('td').data("letter") == $(this).find("div").data("letter")) { 
         console.log('letter is: ' + $(this).find("div").data("letter")) 
         wordIsCorrect++; 
        } 
       }); 
       console.log(spellWord.length + ' , ' + wordIsCorrect); 
       if (spellWord.length == wordIsCorrect) { 
        spellWord.addClass('right-word'); 
        $(right).css('visibility', 'visible'); 
        $(wrong).css('visibility', 'hidden'); 
        score.right++; 
        $('.score').html(score.right + "/2").show(); 
        setTimeout(function() { 
         successSound.play(); 
        }, 200); 

我已經試過的東西等。

spellWord.addClass('td').addClass('right-word'); 

$('.td').addClass('right-word'); 

但似乎無法得到它的工作。小提琴:http://jsfiddle.net/smilburn/Dxxmh/93/

回答

0

如果spellWord元素是TD內,使用parent()

spellWord.parent().addClass('right-word'); 

注意parent()只會給你的直接祖先,用parents(),而不是如果你nede高走。

closest(),siblings()find()也可能有幫助,具體取決於td與spellWord元素相關的位置。

0

嘗試.closest()

spellWord.closest('td').addClass('right-word'); 

tdspellWord.的父。所以你需要去它的td祖先,然後添加類