日期:2014-05-16  浏览次数:20509 次

怎么让setTimeout执行时this所在的指针不是window.
下面代码的console.log(this); //this是指向window了 .而不是li元素本身..要怎么做才能让它指向li
HTML code
<ul id="list">
<li>
<a href="" title="iphone_ios">苹果</a>
<div class="popDiv">

</div>
</li>
<li>
<a href="" title="安卓_java">google</a>
<div class="popDiv">

</div>
</li>
<li>
<a href="" title="wp">微软</a>
<div class="popDiv">

</div>
</li>
<li>
<a href="" title="mysql">甲骨文</a>
<div class="popDiv">

</div>
</li>
<li>
<a href="" title="全球最大的社交网站">facebook</a>
<div class="popDiv">

</div>
</li>
</ul>

var list = document.getElementById("list");
var oli = list.getElementsByTagName("li");
for(var i=0; i<oli.length; i++){
oli[i].onmouseover = function(){
setTimeout(arguments.callee,20);
console.log(this); 
}

}


------解决方案--------------------
JScript code
var list = document.getElementById("list");
var oli = list.getElementsByTagName("li");
for(var i=0; i<oli.length; i++)
{
    oli[i].onmouseover = function(){
        var self = this;
        var callee = arguments.callee;
        setTimeout(function(){callee.call(self)},20);
        console.log(this); 
    }
}