Javascript jQuery:value.attr 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4114870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:39:29  来源:igfitidea点击:

jQuery: value.attr is not a function

javascriptjquery

提问by kennytm

I have this snipped in my page:

我在我的页面中剪下了这个:

$('#category_sorting_form_save').click(function(){
    var elements = $("#category_sorting_elements > div");
    $.each(elements, function(key, value) {
        console.info(key," : ",value);
        console.info("cat_id: ",value.attr('cat_id'));
    });
});

And when it is executed, I get:

当它被执行时,我得到:

0 : <div class="dragable" cat_id="6" value="" style="opacity: 1;">    
value.attr is not a function
    console.info("cat_id: ",value.attr('cat_id'));

What am I doing wrong here? I am trying to get the value of the div.cat_id element.

我在这里做错了什么?我正在尝试获取 div.cat_id 元素的值。

回答by kennytm

Contents of that jQuery object are plain DOM elements, which doesn't respond to jQuery methods (e.g. .attr). You need to wrap the value by $()to turn it into a jQuery object to use it.

该 jQuery 对象的内容是纯 DOM 元素,它不响应 jQuery 方法(例如.attr)。您需要将值包装$()成一个 jQuery 对象才能使用它。

    console.info("cat_id: ", $(value).attr('cat_id'));

or just use the DOM method directly

或者直接使用DOM方法

    console.info("cat_id: ", value.getAttribute('cat_id'));

回答by Gabriele Petrioli

You are dealing with the raw DOM element .. need to wrap it in a jquery object

您正在处理原始 DOM 元素 .. 需要将其包装在 jquery 对象中

console.info("cat_id: ",$(value).attr('cat_id'));

回答by Frédéric Hamidi

The second parameter of the callback function passed to each()will contain the actual DOM element and not a jQuery wrapper object. You can call the getAttribute()method of the element:

传递给each()的回调函数的第二个参数将包含实际的 DOM 元素而不是 jQuery 包装器对象。您可以调用元素的getAttribute()方法:

$('#category_sorting_form_save').click(function() {
    var elements = $("#category_sorting_elements > div");
    $.each(elements, function(key, value) {
        console.info(key, ": ", value);
        console.info("cat_id: ", value.getAttribute('cat_id'));
    });
});

Or wrap the element in a jQuery object yourself:

或者自己将元素包装在 jQuery 对象中:

$('#category_sorting_form_save').click(function() {
    var elements = $("#category_sorting_elements > div");
    $.each(elements, function(key, value) {
        console.info(key, ": ", value);
        console.info("cat_id: ", $(value).attr('cat_id'));
    });
});

Or simply use $(this):

或者干脆使用$(this)

$('#category_sorting_form_save').click(function() {
    var elements = $("#category_sorting_elements > div");
    $.each(elements, function() {
        console.info("cat_id: ", $(this).attr('cat_id'));
    });
});

回答by Eduardo

You can also use jQuery('.class-name').attr("href"), in my case it works better.

您也可以使用jQuery('.class-name').attr("href"),在我的情况下它效果更好。

Here more information: "jQuery(...)" instead of "$(...)"

这里有更多信息:“jQuery(...)”而不是“$(...)”