zoukankan      html  css  js  c++  java
  • 元素设置为display:none,其绑定的事件仍存在

     该文章在2020年1月份进行了重写,文章地址: html元素设置display为none,绑定的事件还存在么

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

    以下是2016年的旧文

    元素设置为display:none,虽然该元素从页面中消失了,其绑定的事件仍存在。

      

    <body>
        <button class="button1">change1</button>
        <button class="button2">change2</button>
        <button class="button3">change3</button>
        <div class="test" style="50px;height:50px;background-color: red;"></div>
        <script>
            $(function () {
                $('.button1').toggle(function() {
                    $('.test').css('background-color', 'black');
                },function () {
                    $('.test').css('background-color', 'red');
                });
                $('.button2').click(function() {
                    $('.test').hide();
                });
                $('.button3').click(function() {
                    $('.test').show();
                });
            })
        </script>
    </body>

     上图是初始页面。change1可以使div在红色和黑色切换,当点击change2时,div消失,然后点击change1,那这次点击能否是消失的div颜色变化呢?揭晓答案,最后,我们点击change3使div出现,发现消失的div颜色变了。所以结论是,元素设置为display:none,其绑定的事件仍存在。

  • 相关阅读:
    验证码缓存问题完美解决方案
    最近项目是跟框架有关的两个问题
    未与信任 SQL Server 连接相关联
    Get请求
    Post请求
    jQuery操作元素
    Dom对象和jQuery包装集
    XMLHttpRequest对象
    jQuery事件与事件对象
    处理数据集
  • 原文地址:https://www.cnblogs.com/zhansu/p/5930466.html
Copyright © 2011-2022 走看看