与字符串对象不工作的jQuery选择器 [英] jQuery selector with string object not working
本文介绍了与字符串对象不工作的jQuery选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var theHTML = '<html><head><title>Hi</title><link rel="apple-icon-touch-precomposed" href="icon.jpg" /></head><body></body></html>';
alert($(theHTML).find('link[rel="apple-icon-touch-precomposed"]').attr('href'));
它警告未定义。我希望它返回icon.jpg。什么是错误的?
It alerts "undefined." I want it to return "icon.jpg". What is wrong?
推荐答案
试试这个:
Try this:
alert($(theHTML).filter('link[rel="apple-icon-touch-precomposed"]').attr('href'));
也就是说,使用 .filter()
而不是 .find()
。
That is, use .filter()
instead of .find()
.
演示: http://jsfiddle.net/WmwRU/
如果您执行控制台。 log($(theHTML))
你会明白为什么。
If you do a console.log($(theHTML))
you'll see why.
这篇关于与字符串对象不工作的jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文