admin管理员组

文章数量:1427333

I have an object that should be a valid jQuery object. When I look at the variable in FireBug it contains all the jQuery functions I'd expect (clone, remove, removeat, etc.). However, I don't see html() as a valid function and when I do this:

stringValue = myjQueryObject.html();

It does fail, saying html() is not a function. However if I do something like this:

stringValue = myjQueryObject[0].innerHTML;

It will correctly pass back the object, minus the parent div and text (which I would expect, seeing as how it is just getting the innerHtml). What do am I missing here?

As noted below, it was an external library that was generating myjQueryObject that had previously returning a valid jQuery object and was updated ... incorrectly. For posterity's sake, I've updated my unit tests to verify that the external library returns a correct jQuery object, make sure this doesn't return null or undefined:

myjQueryObject.jquery

Thanks all! Had a bit of a freakout when my code suddenly broke this morning.

I have an object that should be a valid jQuery object. When I look at the variable in FireBug it contains all the jQuery functions I'd expect (clone, remove, removeat, etc.). However, I don't see html() as a valid function and when I do this:

stringValue = myjQueryObject.html();

It does fail, saying html() is not a function. However if I do something like this:

stringValue = myjQueryObject[0].innerHTML;

It will correctly pass back the object, minus the parent div and text (which I would expect, seeing as how it is just getting the innerHtml). What do am I missing here?

As noted below, it was an external library that was generating myjQueryObject that had previously returning a valid jQuery object and was updated ... incorrectly. For posterity's sake, I've updated my unit tests to verify that the external library returns a correct jQuery object, make sure this doesn't return null or undefined:

myjQueryObject.jquery

Thanks all! Had a bit of a freakout when my code suddenly broke this morning.

Share Improve this question edited Dec 8, 2010 at 16:59 user500038 asked Dec 8, 2010 at 16:35 user500038user500038 4091 gold badge6 silver badges13 bronze badges 5
  • sounds pretty unlikely since jQuerys .html() also uses the .innerHTML property. You should provide the defination of myjQueryObject plus your markup. jsfiddle/4yUqL/14 – jAndy Commented Dec 8, 2010 at 16:45
  • I don't think "myjQueryObject" is a jQuery object: jQuery objects do have html() but they don't have innerHtml() method. How are you creating it? – Andrey Commented Dec 8, 2010 at 16:46
  • @Andrey - OP updated the question to use innerHTML (almost) correctly. @user - You have innerHtml instead of innerHTML. Please post actual code. – user113716 Commented Dec 8, 2010 at 16:48
  • You should link us to the real page ( or, obviously if you can't link to it then replicate it ), which would save us time asking you questions since it seems something is off and there may be multiple libraries involved. – meder omuraliev Commented Dec 8, 2010 at 16:53
  • 1 Okay, it was not returning a jQuery object like I thought it was. To be fair, it was an external templating library that had previously been correctly returning jQuery objects but an update someone made has caused it to no longer return a valid jQuery object. – user500038 Commented Dec 8, 2010 at 16:57
Add a ment  | 

4 Answers 4

Reset to default 2

Either something is modifying the jQuery object prototype, or you have a different library loaded.

Take your object, and test for the jQuery version like this:

alert( myjQueryObject.jquery ); // should give the jQuery version number

EDIT:

Additionally, you state that there's a removeAt method. jQuery doesn't have one of those, unless you mean removeAttr().

Are you sure it's a jquery object? Wrap it in $() again and .html() should exist.

it's [0].innerHTML or .get(0).innerHTML, in that innerHTML is a property not a method.

You should make sure that jquery exists by doing alert( jQuery == $) and you can check for the .jquery property.

That's odd; try $(myjQueryObject).html();. If that works, the object isn't really a jQuery node.

If you still can't figure out why the object lost the html() method, post the code which creates it. Then, we might be able to help.

How are you setting myjQueryObject?

<div id='myElement'></div>

//Good Javascript, Incorrect jQuery
myjQueryObject = document.getElementById('myElement');
myjQueryObject.innerHTML = '<b>My HTML Here</b>';

//Correct jQuery
myjQueryObject = $('#myElement');
myjQueryObject.html('<b>My HTML Here</b>');

//Compact Version of Correct jQuery
$('#myElement').html('<b>My HTML Here</b>');

本文标签: javascriptjQuery object html() doesn39t workbut innerHtml doesStack Overflow