【问题标题】:rel attr with jquery always take the first value带有 jquery 的 rel attr 总是取第一个值
【发布时间】:2012-06-15 12:37:05
【问题描述】:

我有这个:

HTML:

<div id="selectedsongs">
<a href="#" rel="1">song1></a>
<a href="#" rel="2">song2></a>
<a href="#" rel="3">song3></a>
</div>

然后:

selectedBtn = $('#selectedsong');

selectedBtn.click(function()
{
  self.selectedsong($('a', this).attr('rel'));
  return false;
});

但总是取第一个链接的 rel 值,在本例中为“1”。

为什么? :(

非常感谢! ;-)

【问题讨论】:

  • RTFD。 api.jquery.com/attr ".attr() 方法仅获取匹配集中的 first 元素的属性值。"

标签: jquery attr rel


【解决方案1】:

改为这样做:

<div id="selectedsongs">
<a href="#" rel="1">song1></a>
<a href="#" rel="2">song2></a>
<a href="#" rel="3">song3></a>
</div>

$('#selectedsongs a').click(function() {
   alert($(this).attr('rel'));
});

Working example

【讨论】:

    【解决方案2】:
    $('#selectedsongs a').click(function() {
       selectedsong($(this).attr('rel'));
       return false;
    });
    

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2022-01-06
      • 2023-03-30
      • 2011-02-18
      • 1970-01-01
      • 2012-09-15
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多