2014-02-12 43 views
0

我試圖改變內部<span></span>改變類的<i>內部的<span>

整個事情類的<i class="icon-ok">的是這樣的:

<span class="enable"> Some Text 
    <i class="icon-ok"> //This is all there is in here. Just one <i> and nothing else 
</span> 

JQuery的:

$('.enable').live('click', function() { 
    var self = $(this); 
    self./ how do I tell it to look for the i/.removeClass('icon-ok').addClass('icon-remove'); 
}); 

你知道這樣做的方法嗎?

回答

0

使用.find()

self.find('i').removeClass('icon-ok').addClass('icon-remove'); 
0

您可以使用自<i>find()children()是你.enable的孩子:

$('.enable').on('click', function() { 
    var self = $(this); 
    self.find('i').removeClass('icon-ok').addClass('icon-remove'); 
}); 

順便說一句,live已廢棄的jQuery版本1.7。您應該使用on()

0
$(this).find('.icon-ok').removeClass('icon-ok').addClass('icon-remove'); 

$(this).find('i').removeClass('icon-ok').addClass('icon-remove'); 

$(this).children('i').removeClass('icon-ok').addClass('icon-remove'); 

$(this).children('.icon-ok').removeClass('icon-ok').addClass('icon-remove'); 

this keyword

.find()

.children()

0

使用find()搜索點擊的元素裏面......

$('.enable').live('click', function() { 
    var self = $(this); 
    self.find("i").removeClass('icon-ok').addClass('icon-remove'); 
}); 

另外,根據什麼版本的jQuery你使用你可能想給我們on(),而不是live(),因爲這是已被棄用,我相信在最新版本中被刪除。 (我假設可能會有代表團參與,因此首先使用live - 查看更多信息... http://api.jquery.com/on/

相關問題