.attr('class') 在 jQuery() 上未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9575455/
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-27 11:00:03  来源:igfitidea点击:

.attr('class') is undefined on jQuery()

jqueryundefined

提问by jabre

I've got a problem with my jquery function in a wordpress theme. I've used exactly this function severeal times and it worked. The function returns the ID of an Image, that was uploaded via the wordpress uploader.

我在 wordpress 主题中的 jquery 函数有问题。我已经严格使用了这个功能并且它起作用了。该函数返回通过 wordpress 上传器上传的图像的 ID。

Here is the relevant part:

这是相关部分:

// extract the img-ID from class attribute
var jt = jQuery('img', html).attr('class');
alert(html);
alert(jt);
j1 = jt.split(' ');

Both alerts are only there to figure out, what happens. alert(html) returns this:

这两个警报只是为了弄清楚会发生什么。警报(html)返回这个:

<img src="http://www.xyz.com/files/2012/03/stage.jpg" alt="" title="" width="1000" height="371" class="alignnone size-full wp-image-6" />

but alert(jt) returns "undefined".

但 alert(jt) 返回“未定义”。

Any Idea? It would be great, if you could help.

任何的想法?如果您能提供帮助,那就太好了。

Jan

回答by jfriend00

Your use of the context argument with your html variable is incorrect. Per the jQuery documentation, that context argument should be a DOM element, a document or a jQuery object. It cannot be a string of HTML.

您在 html 变量中使用 context 参数是不正确的。根据jQuery 文档,上下文参数应该是 DOM 元素、文档或 jQuery 对象。它不能是 HTML 字符串。

If, what you have is a string and you want to make it into a DOM object, you can do that a number of ways.

如果您拥有的是一个字符串并且您想将它变成一个 DOM 对象,那么您可以通过多种方式来实现。

This code will create a DOM object from the html string you have and then retrieve the class attribute from that DOM object.

此代码将从您拥有的 html 字符串创建一个 DOM 对象,然后从该 DOM 对象中检索 class 属性。

var html = '<img src="http://www.xyz.com/files/2012/03/stage.jpg" alt="" title="" width="1000" height="371" class="alignnone size-full wp-image-6" />';
var item = jQuery(html);
var jt = item.attr('class');
alert(jt);
j1 = jt.split(' ');

You can see it work here: http://jsfiddle.net/jfriend00/phnED/

你可以在这里看到它的工作:http: //jsfiddle.net/jfriend00/phnED/

回答by elclanrs

Your problem is here:

你的问题在这里:

jQuery('img', html)

The context is htmland apparently htmlis an image so you're looking for a imginside an imgwhich it won't find.

上下文是html显然html是一个形象,你正在寻找一个img内部的img,它不会发现。

EDIT:
If htmlis <img src="http://www.xyz.com/files/2012/03/stage.jpg" alt="" title="" width="1000" height="371" class="alignnone size-full wp-image-6" />then you can grab the class like this:

编辑:
如果html是,<img src="http://www.xyz.com/files/2012/03/stage.jpg" alt="" title="" width="1000" height="371" class="alignnone size-full wp-image-6" />那么你可以像这样抓住课程:

var klass = $(html).attr('class');

// You could do this to create an array of all classes
var klasses = $(html).attr('class').match(/\S+/g);

回答by Aram Kocharyan

The context variable htmlyou provided is undefined at that time, and you should define it as the DOM element that the image is a child of.

html您提供的上下文变量当时未定义,您应该将其定义为图像是其子元素的 DOM 元素。

$(document).ready( function() {
// extract the img-ID from class attribute
var html = jQuery('.some-class');
var jt = jQuery('img', html).attr('class');
alert(html);
alert(jt);
j1 = jt.split(' ');
});

http://api.jquery.com/jQuery/

http://api.jquery.com/jQuery/

http://jsfiddle.net/ssUhJ/

http://jsfiddle.net/ssUhJ/

回答by Jirka Kop?iva

without html

没有 html

var jt = jQuery('img').attr('class');
alert(jt);
j1 = jt.split(' ');
alert(j1);

回答by Marat Tanalin

attr('class')does work. Probably your context var (html) is just undefined.

attr('class')确实有效。可能您的上下文 var ( html) 只是未定义。

By the way, to store data, it makes sense to use HTML5 custom data- attributesinstead of classes.

顺便说一下,为了存储数据,使用HTML5 自定义数据属性而不是类是有意义的。

回答by androidavid

When having the given html-string, this should normally work:

当具有给定的 html-string 时,这通常应该有效:

 $(html).attr('class');

回答by Gelin Luo

The problem is at var jt = jQuery('img', html), from jquery document, the second parameter is the context of the jquery object. check the modified program on http://jsfiddle.net/hYKnd/.

的问题是在var jt = jQuery('img', html)jquery的文件,第二个参数是jQuery对象的上下文中。在http://jsfiddle.net/hYKnd/上检查修改后的程序。

回答by Doug Harris

As of jquery 1.6 (see docs for .attr), the .attrswitched from returning an empty string to returning undefined.

从 jquery 1.6(参见.attr 的文档)开始,.attr从返回空字符串切换到返回undefined.

Open this jsfiddle http://jsfiddle.net/QVqXC/and change the jquery version loaded and see how the output message changes.

打开这个 jsfiddle http://jsfiddle.net/QVqXC/并更改加载的 jquery 版本并查看输出消息如何变化。