开发者

Adding image to the particular li

I have a list of li tags with an image. When a user clicks a particular link i want to change the image. When a user clicks a new li tag then the last li tag should reset. I done everything except to set a new image for the particular li tag. And the major problem is i'm not passing any arguments to the function.

$(document).ready(function(){
    $(".sized ul li a").click(function(){
    $('.sized ul li a img').each(function(){$(this).remove(); }); // Removing all images
    $('.sized ul li a ').each(fun开发者_如何学编程ction(){$(this).append('<img src="1.png" />'); });// Reset the images
});


I don't understand why it is a problem to not pass any arguments.

Instead of removing and adding a new image for all links, just change the one that was clicked before. You can change the image by just changing its src attribute with .attr():

$(function() {
    var $prev;
    $(".sized ul li a").click(function(){
        if($prev) {
            $prev.find('img').attr('src', '1.png');
        }
        $prev = $(this).find('img').attr('src', 'new-url-here').end();
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜