Javascript Result of expression is not an object
Using this to get rid of an annoying tiny little 'powered by' box, but it keeps saying 'TypeError: Result of expression 'divs[i]' [undefined] is not an object.' How do I fix it.
function damnwhit开发者_JAVA技巧ebox()
{
var myBody = document.getElementsByTagName("body")[0];
divs = myBody.getElementsByTagName("div");
for(var i in divs)
{
if(divs[i].style.zIndex === 99999)
{
divs[i].style.display == "none";
}
if(divs[i].style.zIndex === "1000")
{
divs[i].style.display == "none";
}
}
}
function damnwhitebox()
{
var myBody = document.getElementsByTagName("body")[0];
divs = myBody.getElementsByTagName("div");
for(var i =0 ; i <divs.length; i++ )
{
if(divs[i].style.zIndex === 99999)
{
divs[i].style.display = "none";
}
if(divs[i].style.zIndex === 1000)
{
divs[i].style.display = "none";
alert(divs[i].innerHTML);
}
}
}
This will work. As in your code you mixed syntax of for in and for loop. Use this one.
For ... in is meant to be used with objects to iterate over their properties. If you are going to use this with array, you have check you haven't added new properties to elements. So better is to avoid this syntax for array traversing.
And you should never use it on DOM array (like in this case) as many JavaScript library do extend DOM.
Also, you probably want to change the == to =
divs[i].style.display = "none";
精彩评论