从 alt 脚本创建图像标题

发布于 2024-08-14 06:59:53 字数 349 浏览 10 评论 0原文

我正在尝试使用这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感性 2024-08-21 06:59:53

您的问题是您在图像存在之前执行代码。

您需要将代码包装在 $(function() { code }) 中,以使其在文档加载后运行。

或者,您可以将

另外,您的 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 the img tags.

Also, your HTML doesn't have a #content element; you need to either change the selector to img or put all of the <img> tags inside a <div id='content'>.

可是我不能没有你 2024-08-21 06:59:53
 $(function(){

   $('a > img[style]').each(function(){
       $el = $(this);
       var style = $el.attr('style');
       $el.attr('style','');
       $el.parent().attr('style',style);
    }); //Moves the inline styles

      $("img").each(function(){
          var title = this.alt;
          $(this).after('<div class="caption">'+ title +'</div>');
      }); //Adds the dynamic captions.
 }); 
 $(function(){

   $('a > img[style]').each(function(){
       $el = $(this);
       var style = $el.attr('style');
       $el.attr('style','');
       $el.parent().attr('style',style);
    }); //Moves the inline styles

      $("img").each(function(){
          var title = this.alt;
          $(this).after('<div class="caption">'+ title +'</div>');
      }); //Adds the dynamic captions.
 }); 
年华零落成诗 2024-08-21 06:59:53

在您的示例页面上,没有 #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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文