【问题标题】:Jquery Each Get AttrJquery 每个获取 Attr
【发布时间】:2015-06-06 13:14:37
【问题描述】:

我正在尝试获取 .each 语句中的元素的属性。

$(document).ready(function(){
    $(':not(select[name=""])').each(function(e) {
        var el = this.attr(name);
        alert('el');
    });
});

因此,如果我有两个匹配的元素,那么我希望它发出两次警报。有人可以帮我解决我的问题。谢谢。

【问题讨论】:

  • 把this.attr(name)改成这个:$(this).attr("name")
  • 和alert('el'); 到alert(el);
  • 做到了。谢谢...我猜只是我草率的代码。

标签: jquery each attr


【解决方案1】:

您所要做的就是将this.attr(name) 更改为$(this).attr("name") 和(如Gary Storey 所说)alert('el'); 更改为alert(el);

【讨论】:

    【解决方案2】:

    首先,this 将引用 DOMElement,而要使用 attr() 方法,您需要获取包含该元素的 jQuery 对象,因此您需要 $(this)。其次,您需要 alert(el) 不带引号。试试这个:

    $(document).ready(function(){
        $(':not(select[name=""])').each(function(e) {
            var el = $(this).attr(name);
            alert(el);
        });
    });
    

    【讨论】:

      【解决方案3】:

      你忘了用 $() 包装选择器

      $(document).ready(function(){
          $(':not(select[name=""])').each(function(e) {
              var el = $(this).attr(name);
              alert(el); // also output the variable not a char
      });
      });
      

      【讨论】:

        猜你喜欢
        • 2020-06-25
        • 1970-01-01
        • 1970-01-01
        • 2015-10-31
        • 1970-01-01
        • 1970-01-01
        • 2014-04-24
        • 2020-11-04
        • 2015-06-24
        相关资源
        最近更新 更多