简体   繁体   中英

onclick image and toggle div using javascript not work?

onclick image and toggle div using javascript not work ?

i change from

    <input type="button" id="Showdiv1" name="Showdiv1" value="Show Div 1" onclick="showDiv('div1')" />

to

<img src="https://developer.apple.com/homekit/images/homekit-hero.png" onclick="showDiv('div1')" />

http://jsfiddle.net/a3MKG/36/

<script>
function showDiv(id) {    
  $("#div1").toggle();
}   

$(document).click(function(e){
    if(!$(e.target).closest('#div1, input[name="Showdiv1"]').length) 
    {
        $('#div1').hide()
    }
})
</script>

Add an ID to your image (below is IMGClick) and change your script to the following :

$("#IMGClick").click(function(){
$("#div1").toggle();
} );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM