从 alt 脚本创建图像标题
我正在尝试使用这个 jQuery 脚本从 alt 标签制作图像标题:
$("#content img").each(function () {
var $this = $(this);
var title = $this.attr("alt");
$this.after('<div class="caption">'+ title +'</div>');
});
我在它之前使用了另一个 jQuery 脚本,它工作正常。我似乎无法让替代脚本的标题起作用。
示例:www.cslack.com/test.html
谢谢,
罗伯特
I am trying to use this jQuery script to make an image caption from the alt tag:
$("#content img").each(function () {
var $this = $(this);
var title = $this.attr("alt");
$this.after('<div class="caption">'+ title +'</div>');
});
I am using another jQuery script before it and it is working fine. I can't seem to get the caption to alt script to work.
Example: www.cslack.com/test.html
Thanks,
Robert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题是您在图像存在之前执行代码。
您需要将代码包装在
$(function() { code })
中,以使其在文档加载后运行。或者,您可以将
块移动到
标记末尾的
img
标记之后。另外,您的 HTML 没有
#content
元素;您需要将选择器更改为img
或将所有![]()
标签放入Your problem is that you're executing your code before the images exist.
You need to wrap the code in
$(function() { code })
to make it run after the document loads.Alternatively, you can move the
<script>
block to the end of the<body>
tag after theimg
tags.Also, your HTML doesn't have a
#content
element; you need to either change the selector toimg
or put all of the<img>
tags inside a<div id='content'>
.在您的示例页面上,没有
#content
这样的东西,这就是它不起作用的原因。如果您在内容周围放置
,那么它应该可以工作。
On your example page, there is no such thing as
#content
which is why it doesn't work.If you place a
<div id="content">
around your content, then it should work.