【问题标题】:Get the "alt" attribute from an image that resides inside an <a> tag从位于 <a> 标记内的图像中获取“alt”属性
【发布时间】:2011-04-22 11:36:46
【问题描述】:

这应该很简单,但由于某种原因,它不起作用。

我想从标签内的图像中获取“alt”属性

<div id="album-artwork">
<ul>
   <li><a href="link to large image"><img src="thumbnail" alt="description of the image" /></a></li>
   ...
</ul>
</div>



$("#album-artwork a").click(function(e) {
e.preventDefault();

var src = $(this).attr("href");
var alt = $(this).next("img").attr("alt");


alert(src); // ok!
alert(alt); //output: undefined

});

任何想法为什么 next() 不起作用?有没有更好的解决方案?

提前致谢 马可

【问题讨论】:

  • 检查我的@user2727841 回答它的测试和工作!!!
  • Marco 检查我的@user2727841 答案...

标签: jquery next


【解决方案1】:

这是因为图片标签在ul标签里面。 next 只查找同一级别的标签。你必须使用

var alt = $(this).children("img").attr("alt");

编辑:包括缺少的附加 "

【讨论】:

  • 我刚刚回答了我自己的问题...忘记了兄弟姐妹和孩子...您的回答有助于确认我的回答。谢谢!
【解决方案2】:
$(this).find('img').attr('alt'); 

【讨论】:

    【解决方案3】:

    尝试$(this).find('img') 来选择链接的一些元素。

    【讨论】:

      【解决方案4】:

      $('img', this).attr('alt');
      

      测试:http://jsbin.com/amado4

      【讨论】:

      • jQuery 在内部将$(child, parent) 重写为$(parent).find(child)
      【解决方案5】:

      没关系。我刚刚明白了...我意识到img 不是&lt;a&gt; 的兄弟姐妹,而是&lt;a&gt; 的孩子

      这是正确的语法

      var alt = $(this).children("img").attr("alt"); 
      

      【讨论】:

        【解决方案6】:

        .next 不会选择下一个元素,即下一个 Li 而不是 IMG?

        【讨论】:

          【解决方案7】:

          试试这个测试和它的工作!!!

          <div id="album-artwork">
          <ul>
             <li><a href="javascript:void(0);"><img src="images/ajax-loader.gif" alt="ajax-loader" /></a></li>
             <li><a href="javascript:void(0);"><img src="images/close.png" alt="close" /></a></li>
             <li><a href="javascript:void(0);"><img src="images/copy.png" alt="copy" /></a></li>
          </ul>
          </div>
          

          jQuery 代码是

              $(document).ready(function() {
                  $("#album-artwork a").click(function(e) {
                      e.preventDefault();
          
                      var src = $(this).attr("href");
                      //var alt = $(this).next("img").attr("alt");
                      var alt = $(this).children().attr("alt");
                      //find function also do the same thing if you want to use.
                      /*var alt = $(this).find("img").attr("alt");*/
          
                      alert(src); // ok!
                      console.log(src); // ok!
                      alert(alt); //output: not undefined now its ok!
                      console.log(alt); //output: not undefined now its ok!
                  });
              });
          

          children() 函数可查找任何子项,但如果您想查找任何特定子项,请定义子项名称,如 children("img") children("button")

          【讨论】:

            【解决方案8】:

            不要复杂:

            $('img').on({'click': function(){
                var alt = this.alt;         
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-01-31
              • 1970-01-01
              • 1970-01-01
              • 2019-10-01
              • 1970-01-01
              相关资源
              最近更新 更多