在20世纪90年代,有一种将Javascript代码直接放入<a>
href属性的方式,如下所示:
<a href="javascript:alert('Hello world!')">Press me!</a>
Run Code Online (Sandbox Code Playgroud)
然后我突然停下来看它.它们全部被以下内容取代:
<a href="#" onclick="alert('Hello world!')">Press me!</a>
Run Code Online (Sandbox Code Playgroud)
对于唯一目的是触发Javascript代码并且没有真正href
目标的链接,为什么鼓励使用onclick
属性而不是href
属性?
我有一些JS代码如下;
var x = self.someAJAXResponseJSON; // x has some object value here
setTimeout(function(x){
console.log("setTimeout ... : " + x); // But x is undefined here
}, 1000);
Run Code Online (Sandbox Code Playgroud)
所以我想将"x"传递给setTimeout回调函数.但我在setTimeout中得到"x"未定义.
我究竟做错了什么 ?
更新
使用DOJO JS修复类似问题的任何想法
setTimeout(dojo.hitch(this, function(){
this.executeSomeFunction(x); // what shud be this
console.log("setTimeout ... : " + x); // But x is undefined here
}), 1000);
Run Code Online (Sandbox Code Playgroud)