2017-06-16 47 views
-1

我有一個div與圖像。當我按鍵盤上的按鍵時,我想要隱藏這些圖像。我怎樣才能做到這一點?更改div內容和刷新div時按鍵盤鍵

<div> 
 
    <span role="checkbox" aria-checked="true" tabindex="0"> 
 
     <img src="checked.gif" role="presentation" alt="" /> 
 
     
 
    </span> 
 
    </div> 
 
    <div> 
 
    <span role="checkbox" aria-checked="true" tabindex="0"> 
 
     <img src="checked.gif" role="presentation" alt="" /> 
 
     
 
    </span> 
 
    </div> 
 
    <div> 
 
    <span role="checkbox" aria-checked="false" tabindex="0"> 
 
     <img src="unchecked.gif" role="presentation" alt="" /> 
 
     
 
    </span> 
 
    </div>

這是div與內容。我想要在keypress事件中隱藏這些圖像..!

+0

用JavaScript – unconnected

+1

需要Javascript爲。有多個問題應該可以幫助你解決你的問題。如[這個答案](https://stackoverflow.com/a/16089470/2412895)或[這個答案](https://stackoverflow.com/questions/2554149/html-javascript-change-div-content)等 – KarelG

+0

HTML代表內容,CSS代表樣式,Javascript代表交互性。你有HTML,現在你需要Javascript。 – Clonkex

回答

0

監聽keyup事件,比.hide()元素。

$(document).ready(function() { 
 
    $('body').keyup(function() { 
 
    $('.img').fadeToggle(); 
 
    //$('.img').hide(); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
    <span role="checkbox" aria-checked="true" tabindex="0" class="img"> 
 
    <img src="http://lorempixel.com/200/200/" role="presentation" alt="" /> 
 
    </span> 
 
</div> 
 
<div> 
 
    <span role="checkbox" aria-checked="true" tabindex="0" class="img"> 
 
    <img src="http://lorempixel.com/200/200/" role="presentation" alt="" /> 
 
    </span> 
 
</div> 
 
<div> 
 
    <span role="checkbox" aria-checked="false" tabindex="0" class="img"> 
 
    <img src="http://lorempixel.com/200/200/" role="presentation" alt="" /> 
 
    </span> 
 
</div>

+0

我想我明白了你的觀點。謝謝。 –

+0

該代碼的問題在於,當您發送密鑰時動畫會排隊。所以在你停止按下一個鍵之後它會繼續淡入淡出。你可以通過添加stop()來修復這個簡單的問題。所以你的JS的第3行看起來像這樣:'$('。img')。stop()。fadeToggle();' –

0

keypress事件嘗試

$(document).on('keypress',function(){ 
 
$('img').hide() 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
<span role="checkbox" aria-checked="true" tabindex="0"> 
 
    <img src="checked.gif" role="presentation" alt="" /> 
 

 
</span> 
 
</div> 
 
<div> 
 
<span role="checkbox" aria-checked="true" tabindex="0"> 
 
    <img src="checked.gif" role="presentation" alt="" /> 
 

 
</span> 
 
</div> 
 
<div> 
 
<span role="checkbox" aria-checked="false" tabindex="0"> 
 
    <img src="unchecked.gif" role="presentation" alt="" /> 
 

 
</span> 
 
</div>

+0

哇..它的工作。 –