下面代码的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); } }
把console.log(this)这句转移到某个由<li/>触发的事件的函数里。
oli[i].onmouseover = function(){
var self = this; setTimeout(arguments.callee,20); console.log(self); }
这个不行啊
setTimeout(arguments.callee,20); 想实现什么样的效果?
可以用JQuery实现啊
$('li').mouseover(function(){
setTimeout(arguments.callee,20);
console.log($(this).text); })
oli[i].onmouseover = function(){ var self = oli[i]; setTimeout(arguments.callee,20); console.log(self); }
试试
for(var i=0; i<oli.length; i++){
oli[i].onmouseover = function(){
var _this=this;
setTimeout(arguments.callee,20);
console.log(_this);
}
oli[i].onmouseover = function(){ var self = oli[i]; setTimeout(arguments.callee,20); console.log(self); }
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事件的元素
}
}
for(var i=0,len=oli.length; i<len; i++){
function(o){
o.onmouseover = function(){ setTimeout(arguments.callee,20); console.log(this); }
}(oli[i])
}
包一层皮就行了啊
function setTimeoutMy(obj,fn,time){
setTimeout(function(){ fn.call(obj);},time);
}