开发者

jquery: dynamically change title (.append()? .html()? or others?)

开发者 https://www.devze.com 2023-03-11 05:47 出处:网络
I would like to change a title dynamically based on a click I have developed on an image slider. I have tried alert to check my code and each click the alert shows a different name which its supposed

I would like to change a title dynamically based on a click I have developed on an image slider. I have tried alert to check my code and each click the alert shows a different name which its supposed to. but when I tried to append() or html() it stays on the first picture name.

var src = $('#slides img').attr('src'); 
$("#image").click(function(){
$("#pic-title").html(src); //this shows the first images' name and never changes.
//$("#pic-title").append(src); //this shows the first images' name and adds on each click, but stays the same name and never changes.
});


<div id="pic-title"></div>

I would like to chan开发者_高级运维ge the image name and not have it repeat.

Any suggestions?

Thank you


You're storing the src variable before the user ever clicks on an image, so it's always going to be the same. Put the var src inside your click function:

$("#image").click(function(){
    var src = this.src;
    $("#pic-title").text(src);
});
0

精彩评论

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

关注公众号