2017-09-07 137 views
0

我想從onclick事件傳遞「this」到一個函數,而不是傳遞給另一個函數,它的第一個函數工作,但「this」沒有傳遞到從第一個函數調用的下一個函數?如何將onclick事件傳遞給多個函數?

這是我的代碼,請指教?

function hideProcessing(nextThis) { 
 
    nextThis.parentNode.parentNode.style.display = "none"; 
 
    nextThis.parentNode.style.display = "block"; 
 
} 
 

 
function showProcessing(param) { 
 
    param.parentNode.parentNode.style.display = "block"; 
 
    param.parentNode.style.display = "none"; 
 
    var thisElement = this 
 

 
    setTimeout('hideProcessing(thisElement)', 3000); 
 
}
<div> 
 
    <span id="overlap">show me</span> 
 
    <div id="sendContainer" class="sendContainer"> 
 
    <button onclick="showProcessing(this);">send</button> 
 
    </div> 
 
</div>

+0

你有這個存儲在參數中。你分配thisElement = this時出錯了。你也不應該用string來調用setTimeout,應該有一個函數作爲第一個參數。在這個函數中,你仍然可以訪問'param',這是你的。 –

+0

試試這個.name或者那樣的id。 https://stackoverflow.com/questions/4195970/what-does-this-mean – zod

回答

0

首先你應該糾正 '這' 在你的onclick功能PARAM 其次打電話給你的setTimeout函數如下:

setTimeout(function(){ 
    hideProcessing(thisElement) 
}, 3000); 

setTimeout函數接受一個函數作爲輸入不是字符串

DEMO:http://jsbin.com/wopeyosovu/2/edit?html,js,console,output

0

嘗試設置超時用包裝材料的功能,也this,在函數的範圍,是該窗口。

嘗試:

function hideProcessing(nextThis) { 
    nextThis.parentNode.parentNode.style.display = "none"; 
    nextThis.parentNode.style.display = "block"; 
} 

function showProcessing(param) { 
    param.parentNode.parentNode.style.display = "block"; 
    param.parentNode.style.display = "none"; 

    setTimeout(function(){//wrapper anonymous function 
     hideProcessing(param); 
    }, 3000); 
} 

this(雙關語意),瞭解多一點了解範圍以及如何以及何時使用this

0

function hideProcessing(nextThis){ 
 
console.log('nextThis', nextThis); 
 
nextThis.parentNode.parentNode.style.display = "none"; 
 
nextThis.parentNode.style.display = "block"; 
 

 
} 
 
    
 
function showProcessing(param){ 
 
param.parentNode.parentNode.style.display = "block"; 
 
param.parentNode.style.display = "none"; 
 

 
setTimeout(hideProcessing(param), 3000); 
 
}
<div> 
 
    <span id="overlap">show me</span> 
 
    <div id="sendContainer" class="sendContainer"> 
 
     <button onclick="showProcessing(this);" >send</button> 
 
    </div> 
 
</div>

相關問題