【问题标题】:on click working from console but not in code单击从控制台工作但不在代码中
【发布时间】:2018-03-13 07:19:05
【问题描述】:

我在 ready() 函数中编写了一个点击 jquery 函数,但它仍然无法在网站上运行。当我将代码粘贴到控制台时,它确实有效。

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

}); 
});

【问题讨论】:

  • 你能创作小提琴吗?
  • 如果您提供一个工作代码 sn-p 重现所描述的问题,我们可能会有适当的机会了解发生了什么。

标签: javascript jquery console


【解决方案1】:

试试这个代码。

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

}); 
});

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

谢谢。

【讨论】:

  • 嗨。我试过这个。即使这只是从控制台工作,而不是通过添加代码。
  • 成功了。我试图在页面完全加载之前点击。
  • 很高兴它有帮助。 :) @FahadUddin
【解决方案2】:

$(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

【讨论】:

  • 谢谢。问题是我在页面完全加载之前点击了。
猜你喜欢
  • 2015-05-27
  • 2020-03-14
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 2020-11-06
  • 2022-09-28
相关资源
最近更新 更多