在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ js 增加鼠標(biāo)點(diǎn)擊監(jiān)聽(tīng)事件,為什么點(diǎn)擊了無(wú)效也不報(bào)錯(cuò)?

js 增加鼠標(biāo)點(diǎn)擊監(jiān)聽(tīng)事件,為什么點(diǎn)擊了無(wú)效也不報(bào)錯(cuò)?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #cs2{
            height: 800px;
            width: 500px;
            background-image: url("./psd001.png");
            text-align: center;
            display: none;
        }

        #cs1{
            height: 350px;
            width: 200px;
        }
    </style>
</head>
<body>

<div id='cs1'>
    <img src="vv.png">
</div>


<div id='cs2'>
    123
</div>


<script>
    /*
    function show()
    {
        console.log('鼠標(biāo)點(diǎn)解了')
    }
    */


    function show(n)
    {
        console.log('鼠標(biāo)經(jīng)過(guò)了')
        document.getElementById(n).style.display="block";
    }


    function hide(n)
    {
        document.getElementById(n).style.display="none";
    }

    //document.getElementById('cs1').onmouseover = show('cs2');

    //document.getElementById('cs1').onmouseout=hide('cs2');

    var cs1 = document.getElementById('cs1');
    if(cs1.attachEvent){
        cs1.attachEvent('onclick',function () {//添加一個(gè)單擊事件(單擊時(shí)觸發(fā)function函數(shù))
            show('cs2')
        });
    }else if(cs1.addEventListener) {
        cs1.addEventListener('onclick',function () {//添加一個(gè)單擊事件(單擊時(shí)觸發(fā)function函數(shù))
            show('cs2')
        });
    }




</script>



</body>
</html>
回答
編輯回答
歆久

click和onclick是兩種用法,onclick是在html代碼中聲明對(duì)此html代碼綁定一個(gè)點(diǎn)擊事件,但是在js里面是需要使用click來(lái)聲明點(diǎn)擊事件

例如
html: <input type='test' onclick="function();">
js: cs1.attachEvent('onclick',function());

2018年7月14日 21:13
編輯回答
北城荒
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #cs2{
            height: 800px;
            width: 500px;
            background-image: url("./psd001.png");
            text-align: center;
            display: none;
        }

        #cs1{
            height: 350px;
            width: 200px;
        }
    </style>
</head>
<body>

<div id='cs1'>
    <img src="vv.png">
</div>


<div id='cs2'>
    123
</div>


<script>
    /*
    function show()
    {
        console.log('鼠標(biāo)點(diǎn)解了')
    }
    */


    function show(n)
    {
        console.log('鼠標(biāo)經(jīng)過(guò)了')
        document.getElementById(n).style.display="block";
    }


    function hide(n)
    {
        document.getElementById(n).style.display="none";
    }

    //document.getElementById('cs1').onmouseover = show('cs2');

    //document.getElementById('cs1').onmouseout=hide('cs2');

    var cs1 = document.getElementById('cs1');
    if(cs1.attachEvent){
        cs1.attachEvent('onclick',function () {//添加一個(gè)單擊事件(單擊時(shí)觸發(fā)function函數(shù))
            show('cs2')
        });
    }else if(cs1.addEventListener) {
        cs1.addEventListener('click',function () {//添加一個(gè)單擊事件(單擊時(shí)觸發(fā)function函數(shù))
            show('cs2')
        });
    }




</script>



</body>
</html>
2017年12月5日 20:23
編輯回答
伐木累
cs1.attachEvent('onclick',function(){});
//是 click 不是 onclick
cs1.addEventListener('click',function(){});
2018年4月16日 18:24