开发者

Why can't I assign a variable inside a function in Javascript?

Very new to JS here. When I write PHP I have no problem assigning variables inside开发者_运维知识库 a function, but inside a JavaScript function it doesn't work. Why?

example:

function hello() {

var animal = 'Dog';
document.write(animal);

}


Are you calling the hello() function anywhere? If not, you will not execute the contents of the function and thus, no write will happen.


put this snippet inside your function to check if your function is being called

alert('snippet');

If a message box appears, your code should work, but if it does not then the function is not being executed, post some html coding also.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜