Jquery 每人获得属性

Jquery Each Get Attr

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

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

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

您忘记用 $()

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

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

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

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