2014-02-12 22 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/

相关问题