我已经动态添加了新的li
标签,并且他们假设根据活动的类显示不同的图像,这些图像在点击时切换,但我无法掌握它。我知道因为应该更改的图片是img标签,但更改src
标记img
的条件是li
标记中的活动类
我尝试在jquery中使用以下代码
if ($("li").hasClass("active"))
{
$("img#test2").attr("src","assets/img/button_home_selected3.png");
}
else
{
$("img#test2").attr("src","assets/img/button_home_plain.png");
}
Html是:
<li id="test" style="list-style: none;" class="active">
<a href="#home" data-toggle="tabs"><img src="assets/img/button_home_plain.png" id="test2" width="83" style="margin-left:10px;"</a>
</li>
事件处理程序:
$('.plus').click(function(e) {
e.preventDefault();
var li_count = $('#nav-tabs li').length;
if (li_count <= 3)
if(counter <= 3){
$('#nav-tabs').append('<a href="#tab' + counter + '" data-toggle="tabs"><li style="list-style: none; float:left; margin-left:-5px; width:82px;"><img src="assets/img/button_home_plain.png" width="83" style="margin-left:18px;" /></a></li>');
} else { alert("Only 3 Tabs Allowed!")};
答案 0 :(得分:0)
试试这个:
...
...
//code where you are changing the class
...
...
$('li').each(function() {
if($(this).class().indexOf("active") !== -1)
{
//Active class is applied
$(this).children().children().attr("src", "assets/img/button_home_selected3.png");
}
else
{
$(this).children().children().attr("src", "assets/img/button_home_plain.png");
}
});
...
...