Js绑定事件的几种方式

1.直接在标签中绑定

<!DOCTYPE html>
<html>
    <head>
        <title>直接在标签里绑定事件</title>
    </head>
<body>

    <input type="button" value="点我触发事件" onclick="fun1('liming')" />

    <script type="text/javascript">
        function fun1(name)
            {
                alert("欢迎:"+name)
            }
    </script>

</body>

2.querySelector方法绑定

<!DOCTYPE html>
<html>
<head>
    <title>querySelector方法绑定</title>
</head>
<body>

    <input type="button" id="b1" value="点我触发事件"/>

    <script type="text/javascript">
        function fun1(name)
        {
            alert("欢迎:"+name)
        }
        document.querySelector('#b1').onclick=function (){fun1('lili')}

    </script>

</body>
</html>

3.getElementByID方法绑定

<!DOCTYPE html>
<html>
<head>
    <title>getElementByID方法绑定</title>
</head>
<body>

    <input type="button" id="b1" value="点我触发事件"/>

    <script type="text/javascript">
        function fun1(name)
            {
                alert("欢迎:"+name)
            }
        document.getElementById('b1').onclick=function (){fun1('lili')}
    </script>

</body>
</html>

4.JQuery的原生方法绑定

<!DOCTYPE html>
<html>
<head>
    <title>JQuery的原生方法绑定</title>
    <script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>

    <input type="button" id="b1" value="点我触发事件"/>

    <script type="text/javascript">
        function fun1(name)
            {
                alert("欢迎:"+name)
            }
        $('#b1').click(function (){fun1('lili')});
    </script>

</body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值