开发者

jQuery object html() doesn't work ... but innerHtml does

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 thi开发者_Python百科s:

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.


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>');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜