.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
.attr('class') is undefined on jQuery()
提问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 html
and apparently html
is an image so you're looking for a img
inside an img
which it won't find.
上下文是html
显然html
是一个形象,你正在寻找一个img
内部的img
,它不会发现。
EDIT:
If html
is <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 html
you 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(' ');
});
回答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 .attr
switched 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 版本并查看输出消息如何变化。