代码之家  ›  专栏  ›  技术社区  ›  user379888

单击“从控制台但不在代码中工作”

  •  0
  • user379888  · 技术社区  · 7 年前

    我在ready()函数中编写了一个点击jquery函数,但它在站点上仍然不起作用。当我将代码粘贴到控制台中时,它确实起作用。

    jQuery(document).ready(function($){
        jQuery(".product-video-popup").click(function(){
            jQuery("html, body").animate({ scrollTop: 1250 }, 2000);
    
    }); 
    });
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   SRK    7 年前

    请尝试此代码。

    jQuery(document).ready(function($){
        jQuery(document).on("click",".product-video-popup",function(){
            jQuery("html, body").animate({ scrollTop: 1250 }, 2000);
    
    }); 
    });
    

    让我知道它是否有效。动态更新DOM时可能会出现这种情况。请检查。

    谢谢

        2
  •  1
  •   Manoj Kumar    7 年前

    $(document).ready(function(){
        $("button").click(function(){
           jQuery("div").animate({ scrollTop: 1250 }, 1000);
        });
    });
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
    </script>
    </head>
    <body>
    
    <div style="border:1px solid black;width:100px;height:150px;overflow:auto">
    This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.</div><br>
    
    <button>Return the vertical position of the scrollbar</button>
    <p>Move the scrollbar down and click the button again.</p>
    
    </body>
    </html>

    上述代码工作正常。 我认为您所犯的错误是创建DOM元素或不包括jquery库。

    我提到的工作如下。 Js fiddle link for scroll down