Open orslumen opened 13 years ago
I'm having a different problem: I have a HTML file like the following:
<script language="javascript">
document.getElementById('ex').innerHTML = '\
<table class="table">\
<tr>\
<th class="sorting"><a href="#">login</a></th>\
</tr>\
</table>';
</script>
It just replaces an innerHTML of a div with some data. After loading this page:
console.log("Link elements: " + document.getElementsByTagName("a").length);
This shows me "Link elements: 0", which is wrong - expected to be 1. However, if I try:
var elements = [];
var all = document.getElementsByTagName("*");
for(var i = 0; i < all.length; i++) {
if(all[i].tagName === 'A') {
elements.push(all[i]);
}
}
console.log("Link elements filtered: " + elements.length);
It gives me the correct number of elements (in this case, 1).
One of my jQuery plugins uses the
$.wrap()
function. After that method is called, thegetElementsByTagName
function returns incorrect results. It completely ignores the new element and moves the wrapped element to the end of the list.To reproduce (without jQuery):
This produces the following results: Before:
As you can see the innerHTML is correct, but the
container.getElementsByTagName("*")
is missing the #wrapper div and has incorrectly moved the #to_be_wrapped div to the end.Fortunately the
childNodes
property does contain the correct state, so as a workaround I switched back to the old recursive implementation of getElementsByTagName using childNodes, which seems to work fine for the moment.