我試圖通過「這一」來源:JavaScript傳遞元素,但接收窗口?
myVar = setInterval("displayDate(this)",1000);
,並通過「div.test」像它應該當我通過它一步,但在接受它的時候:
function displayDate(obj){
}
它說它是「窗口」?以下是我正在構建的JavaScript。我正在嘗試爲觸發事件的類構建基礎,並最終通過Sprite解析將變量(現在設置爲100)更改爲elements.src =「。jpg」。但我目前堅持這一點,我不想在.html代碼中插入onmousemove屬性等來保持它的清潔。 。 。請記住,這只是我寫第三天.html/.css/.js所以任何幫助表示讚賞!
// This helps create a static variable that isn't polluting the global namespace
var incr = (function() {
var i = 0;
return function(){ return i++; };
})();
// This perform all of the functions that we would like with some error handling
function displayDate(obj){
var counter = incr();
try{
obj.innerHTML=counter;
}catch(err){
var txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
}
}
// This is our trigger that sets an interval for our main Java function
$(function(){
var myVar;
$(".test").hover(function() {
// The mouse has entered the element, can reference the element via 'this'
myVar = setInterval("displayDate(this)",100);
},function() {
// The mouse has left the element, can reference the element via 'this'
clearInterval(myVar);
}
);
});
爲什麼「displayDate(this)」在引號中? – dezman
你不應該傳遞字符串setTimeout或setInterval –
沃森你是對的,我給你加1!謝謝 – user2368363