【问题标题】:jquery: dynamically change title (.append()? .html()? or others?)jquery:动态更改标题(.append()?.html()?或其他?)
【发布时间】:2011-09-11 10:20:36
【问题描述】:

我想根据我在图像滑块上开发的点击动态更改标题。我已经尝试通过警报检查我的代码,每次单击警报都会显示一个不同的名称。但是当我尝试 append() 或 html() 时,它会停留在第一个图片名称上。

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>

我想更改图片名称,不要重复。

有什么建议吗?

谢谢

【问题讨论】:

  • 你能把这个引用的html也加进去吗?
  • 我想这就是你所说的 html 的意思?我添加了输出标题名称时应该引用的div标签。
  • 你为什么使用图像src attr,为什么不使用rel、alt或title?
  • 这只是一个例子来说明我的观点
  • 我想很多人确实将文件名用作一种标题,只是好奇。

标签: jquery html image dynamic append


【解决方案1】:

您在用户单击图像之前存储了src 变量,因此它总是相同的。将var src 放入您的点击函数中:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多