简体   繁体   中英

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. But everytime I invoke a jQuery function on a selected object, I get the error:

"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.

You are no longer working with jQuery objects when using square braces.

jQuery("li")[0]

This returns you the 1st li as a DOMElement, not a jQuery object.

jQuery("li")[0].children[0]

This returns the 1st li 's 1st child as a DOMElement, not a jQuery object.

.html()

This function only works for jQuery objects. For DOMElements, you can use the .innerHTML property.

I suggest instead of dealing with DOMElements, you should continue working with jQuery objects. 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] . Try wrapping that in a jQuery object and then calling 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. Without seeing your HTML i can't recommend a better selector but a cheap and dirty fix would be

jQuery(jQuery('li')[0].children[0]).html();

This will convert the DOM object result into a jQuery object which has the .html() function.

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. You probably want to use eq() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM