Problem exploring a DOM loaded from a string.
I've been confronted with the following problem:
- I have a ajax call returning a content.
- A callback method receives the content.
- I load the content using $(data).
- When I look for a particular element using $("name",data), the
script stops with the following error:
ret[i].getElementsByTagName is not a function.
I've checked everywhere, without fining any error. At the end I ran
the following test:
function test() {
var test1="<html><head><title>A test</title></head><body>Hello
there
</body></html>";
var body1=$("body",test1); // This is ok.
alert("body1="+body1);
var test2="<html><head><title>A test</title></head><body>Hello
hereHello there
</body></html>";
var body2=$("body",test2); // Error:
ret[i].getElementsByTagName is not a function.
alert("body2="+body2);
}
The problem appears if any node in the Xml has, at the same level,
'text' nodes and 'element' nodes.
My interpretation of this problem is that the "find" method was
assuming that all child nodes are 'elements', when some of them can be
'text' elements.
To correct that I've made the following change in jQuery 1.1.4
(starting at line 1190):
// Handle IE7 being really dumb about <object>s
if ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" )
tag = "param";
if (typeof ret[i]=="object") // <=== Added this line.
if (ret[i].nodeType!=3) // <=== Added this line.
r = jQuery.merge( r, ret[i].getElementsByTagName( tag ));
I'm wondering if any one has found the same problem, and if it is
actually a bug in jQuery, or I am doing something wrong (I'm new to
jQuery)
Regards,
A. Huygens