2017-09-07 20 views
0

私はonclickイベントから "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

uこれはparamに格納されています。 thisElement = thisを間違えて指定しました。また、setTimeoutをstringで呼び出すべきではありません。最初のパラメータとして関数が存在するはずです。その機能では、あなたはまだあなたの 'param'にアクセスすることができます。 –

+0

これを試してみてください。 https://stackoverflow.com/questions/4195970/what-does-this-mean – zod

答えて

0

まず、あなたのonclickの機能で 'これは' は次のようにあなたのsetTimeout関数を呼び出す第二に をPARAMすることを修正する必要があります。

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

setTimeout関数は、としての機能を取ります文字列を入力しないでください

デモ: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>

関連する問題