开发者

GetElementById is not a function?

When running this script i get an error message that document.GetElementById is not a function?? - You can see the site at: http://www.fritidsjobbere.dk:

//Show开发者_开发百科 random div with Javascript
antal = 6;
var randomnumber=Math.floor(Math.random()*antal);
if(randomnumber == "0") {
randomnumber = 1;
}
//alert(randomnumber);
document.getElementsById("partner-ad"+randomnumber).style.display = 'block'; 


getElementsById -> getElementById


Are you confusing getElementById and getElementsByTagName. Note the former is singular, and the latter is plural.

Of course, if you're using jQuery (as your tag intimates), you needn't be using either, as you can just use the #ID selector:

$("#partner-ad"+randomnumber").show();


It's singular (IDs are unique, it should only return one result), document.getElementById like this:

document.getElementById("partner-ad"+randomnumber).style.display = 'block'; 

Also, your actual code on the site differs from your question, it's document.getElementsByID on the actual page...make sure that ID is Id as well.


Another side-note, since your ads are partner-ad1 through partner-ad6, your current code would never show the 6th ad, you can simplify it and fix that issue like this:

var antal = 6;
var randomnumber=Math.ceil(Math.random()*antal); //instead of Math.floor
document.getElementById("partner-ad"+randomnumber+"").style.display = 'block';


document.getElementsById("partner-ad"+randomnumber).style.display = 'block';

Should be

document.getElementById("partner-ad"+randomnumber).style.display = 'block';

Why not do this using jQuery by the way?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜