javascript .children not working in Camino Browser
This is an odd one, for whatever reason, getting the children of an element doens't work in Camino browser. Works in all other browsers. Anyone know how to fix this? Google is no help :(
var site_result_content = document.getElementById(content_id);
site_child_nodes = site_result_content.children;
alert('started');
for(i=0;i<site_child_nodes.length;i++) {
alert('cycle1');
document.getElementById(site_child_nodes[i].id).className = 'tab_content';
ShowHide(site_child_nodes[i].id,'hidden开发者_StackOverflow');
}
In this case, the started alert is called, but the cycle1 isn't.
Use childNodes
instead. children
started out as a proprietary property that in IE, whereas childNodes
is in the W3C DOM spec and is supported by every major browser released in the last decade. The difference is that children
contains only elements whereas childNodes
contains of all types, in particular text nodes and comment nodes.
I've optimized your code below. You should declare all your variables with var
, including those used in loops such as i
. Also, document.getElementById(site_child_nodes[i].id)
is unnecessary: it will fail if the element has no ID and is exactly the same as site_child_nodes[i]
otherwise.
var site_result_content = document.getElementById(content_id);
var site_child_nodes = site_result_content.childNodes;
alert('started');
for (var i = 0, len = site_child_nodes.length; i < len; ++i) {
if (site_child_nodes[i].nodeType == 1) {
alert('cycle1');
site_child_nodes[i].className = 'tab_content';
ShowHide(site_child_nodes[i].id, 'hidden');
}
}
I'd hazard a guess that it's not been implemented yet (it was only implemented in Firefox 3.5). You can use childNodes
instead, which returns a list of nodes (rather than just elements). Then check nodeType
to make sure it's an element.
var site_result_content = document.getElementById(content_id);
site_child_nodes = site_result_content.childNodes;
alert('started');
for(i=0;i<site_child_nodes.length;i++) {
// Check this is actually an element node
if (site_child_nodes[i].nodeType != 1)
continue;
alert('cycle1');
document.getElementById(site_child_nodes[i].id).className = 'tab_content';
ShowHide(site_child_nodes[i].id,'hidden');
}
精彩评论