首页 新闻 会员 周边 捐助

怎么让setTimeout执行时this所在的指针不是window.

0
悬赏园豆:20 [待解决问题]

下面代码的console.log(this); //this是指向window了 .而不是li元素本身..要怎么做才能让它指向li

<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); 
}

}
< >
分享
所有回答(11)
0

把console.log(this)这句转移到某个由<li/>触发的事件的函数里。

sinhbv | 园豆:2579 (老鸟四级) | 2012-05-31 08:22
1
oli[i].onmouseover = function(){
var self = this;
setTimeout(arguments.callee,20); console.log(self); }
路人已 | 园豆:228 (菜鸟二级) | 2012-05-31 09:02

这个不行啊

支持(0) 反对(0) 深蓝色梦想 | 园豆:6 (初学一级) | 2012-05-31 14:45
0

setTimeout(arguments.callee,20); 想实现什么样的效果?

dudu | 园豆:29568 (高人七级) | 2012-05-31 13:21
0

可以用JQuery实现啊  

$('li').mouseover(function(){

  setTimeout(arguments.callee,20);
  console.log($(this).text);
})

┢┦偉 | 园豆:1240 (小虾三级) | 2012-06-02 11:22
0
oli[i].onmouseover = function(){
var self = oli[i];
setTimeout(arguments.callee,20);
console.log(self); 
}

 

试试
十年灯 | 园豆:356 (菜鸟二级) | 2012-06-08 11:44
0

for(var i=0; i<oli.length; i++){
oli[i].onmouseover
= function(){

var _this=this;

setTimeout(arguments.callee,20);
console.log(_this);
}

unbreakable | 园豆:111 (初学一级) | 2012-06-09 06:50
0
oli[i].onmouseover = function(){
var self = oli[i];
setTimeout(arguments.callee,20);
console.log(self); 
}

codeyuyu | 园豆:224 (菜鸟二级) | 2012-07-06 15:41
0

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

  ev = ev || window.event; // 事件    
      var  targetThis = ev.target || ev.srcElement; // 获得事件源元素

//targetThis 这个就是你当前触发onmouseover事件的元素
}

}

kingNull | 园豆:209 (菜鸟二级) | 2012-07-09 15:43
0
for(var i=0,len=oli.length; i<len; i++){
function(o){
o.onmouseover = function(){
setTimeout(arguments.callee,20);
console.log(this); 
}
}(oli[i])
}
mishe | 园豆:202 (菜鸟二级) | 2012-07-30 13:37
0

包一层皮就行了啊

function setTimeoutMy(obj,fn,time){

  setTimeout(function(){ fn.call(obj);},time);

}

HeiMan | 园豆:233 (菜鸟二级) | 2013-11-28 13:21
0
oli[i].onmouseover = fn;
function fn(){
  console.log(self);
  var self = this;
  setTimeout(function () {
    fn.apply(self, []);
  },20);
  
}
 
bdbk | 园豆:202 (菜鸟二级) | 2016-07-28 17:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册