[英]jQuery in console not working properly
I'm using the jQueryify bookmarklet on a page so that I can call jQuery functions from the console. 我正在页面上使用jQueryify bookmarklet,以便我可以从控制台调用jQuery函数。 But everytime I invoke a jQuery function on a selected object, I get the error:
但每次我在所选对象上调用jQuery函数时,都会收到错误:
"TypeError: jQuery("li")[0].children[0].html is not a function
[Break On This Error] jQuery('li')[0].children[0].html();
I have tried this in FireBug as well as Google Chrome's Webkit console. 我在FireBug以及Google Chrome的Webkit控制台中尝试过这个。
You are no longer working with jQuery objects when using square braces. 使用方括号时,您不再使用jQuery对象。
jQuery("li")[0]
This returns you the 1st li
as a DOMElement, not a jQuery object. 这将返回第一个
li
作为DOMElement,而不是jQuery对象。
jQuery("li")[0].children[0]
This returns the 1st li
's 1st child as a DOMElement, not a jQuery object. 这将第一个
li
的第一个子节点作为DOMElement返回,而不是jQuery对象。
.html()
This function only works for jQuery objects. 此函数仅适用于jQuery对象。 For DOMElements, you can use the
.innerHTML
property. 对于DOMElements,您可以使用
.innerHTML
属性。
I suggest instead of dealing with DOMElements, you should continue working with jQuery objects. 我建议您不要处理DOMElements,而应继续使用jQuery对象。 Try using this instead:
请尝试使用此代码:
jQuery('li').eq(0).children().eq(0).html()
It looks like you are trying to call a jQuery function, html
, on a DOM object children[0]
. 看起来你试图在DOM对象
children[0]
上调用jQuery函数html
。 Try wrapping that in a jQuery object and then calling html
尝试将其包装在jQuery对象中,然后调用
html
var temp = jQuery("li")[0].children[0];
var html = jQuery(temp).html();
Check the result of jQuery("li")[0].children[0]
, it's a regular DOM object NOT a jQuery object. 检查
jQuery("li")[0].children[0]
,它是一个常规DOM对象而不是jQuery对象。 Without seeing your HTML i can't recommend a better selector but a cheap and dirty fix would be 没有看到你的HTML我不能推荐一个更好的选择器,但一个廉价和肮脏的修复将是
jQuery(jQuery('li')[0].children[0]).html();
This will convert the DOM object result into a jQuery object which has the .html()
function. 这会将DOM对象结果转换为具有
.html()
函数的jQuery对象。
Try following 试试以下
jQuery(jQuery("li")[0].children[0]).html();
or better one 或更好的一个
jQuery("li:eq(0)").children(':eq(0)').html();
or another one 或另一个
jQuery("li:eq(0)").children().eq(0).html();
even this one will work 即使这个也行
jQuery("li").eq(0).children().eq(0).html();
Accessing the array elements on the jquery object (using []) returns a DOMElement, which obviously doesn't have jquery's methods. 访问jquery对象上的数组元素(使用[])会返回一个DOMElement,它显然没有jquery的方法。 You probably want to use
eq()
instead. 您可能想要使用
eq()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.