0

我已经动态添加了新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!")};
4

1 回答 1

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");
    }
});

...
...
于 2013-02-08T09:50:22.510 回答