简体   繁体   English

.click()在页面加载时不起作用,但在控制台中工作

[英].click() doesn't work on page load but works in console

Here is my code: 这是我的代码:

<script>
  document.getElementById("test").click();
</script>

Is there a reason why it doesn't trigger the click(); 有没有理由不触发click(); on page load but when I do it in the console for on Firefox, it works. 在页面加载,但当我在Firefox的控制台中这样做时,它的工作原理。 Why is that and how do i fix it? 为什么这样,我该如何解决?

EDIT: I have also tried this: 编辑:我也试过这个:

<script>
  $(document).ready(function() {
    $('#test').trigger('click');
 });
</script>

still doesn't work... 仍然不起作用......

Since you haven't post your whole page, I think the highest probability is that the "test" element is not loaded when the script is executed. 由于您没有发布整个页面,我认为最高的概率是在执行脚本时不加载“test”元素。 If you want to ensure your scripts get's executed, you should use 如果你想确保你的脚本被执行,你应该使用

$( document ).ready(function() {
    $("#test").click();
});

Working Example: 工作实例:

</!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
        function printTest() {
            alert('test')
        }
    </script>
</head>
<body>
<script type="text/javascript">
    $( document ).ready(function() {
        $("#test").click();
    });
    // document.getElementById("test").click();
</script>
<button id="test" onclick="printTest()"> Click Me</button>
</body>
</html>

Without seeing the rest of your code, I need to guess here. 没有看到你的其余代码,我需要在这里猜测。 Given that your code fails when in the document, yet functions as expected when entered into the console after the page has loaded, it's reasonably safe to assume the DOM hasn't finished loading yet. 鉴于您的代码在文档中失败,但在页面加载后输入控制台时按预期运行,假设DOM尚未完成加载是相当安全的。

To deal with this, we attach a function to the load event of the window object. 为了解决这个问题,我们将一个函数附加到window对象的load事件中。 Once the HTML/CSS/JS files have been loaded and parsed, the event is fired. 一旦加载并解析了HTML / CSS / JS文件,就会触发该事件。 We handle that event by locating and clicking the desired button. 我们通过查找并单击所需按钮来处理该事件。

 window.addEventListener('load', onDocLoaded, false); function onDocLoaded(evt) { document.getElementById('test').click(); } 
 <button id='test' onclick='alert("you clicked the test button");'>Click me</button> 

keep in mind to include the jquery library befor you do others. 请记住,包括jquery库,因为你做其他人。 this jquery google CDN provide different version of jquery online cdn. 这个jquery google CDN提供了不同版本的jquery online cdn。 use one of them and hope following helps to you. 使用其中之一,希望以下对您有所帮助。

refer this working demo. 参考这个工作演示。 .... ....

 <html> <head></head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <style> #mybutton{ width: 100px; height: 50px; color: white; background-color: orange; border: 0; border-radius: 5px; font-family: monospace; cursor: pointer; } </style> <body> <button id="mybutton">Click me to see th alert</button> <script type="text/javascript"> $("#mybutton").click(function(){ alert("jquery and click event is working fine. ..."); }); </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript中的click()无法正常运行,但在控制台中可以正常运行 - click() in javascript doesn't work but in console it works fine Puppeteer page.click 有效,但 page.evaluate + 文档单击不起作用 - Puppeteer page.click works, but page.evaluate + document click doesn't work jquery命令在控制台上成功运行,但是在添加click()时不起作用,但也未显示任何错误 - jquery command works successfully on console but does not work when click() is added but doesn't show any errors either 单击控制台中的按钮不起作用 - Click on a button in the console doesn't work jQuery AJAX请求不适用于页面加载,但是可以通过调试控制台运行 - jQuery AJAX Request Doesn't Work on Page Load, However, It Does Work from Debug Console jQuery Click-在第1页加载时不起作用,但是在重新加载页面时起作用 - jQuery Click - Doesn't work when page 1st loaded, but works if page is reloaded innerHTML 在第一次单击按钮时有效,但在第二次单击时无效 - innerHTML works on first button click but doesn't work on second click 返回不起作用,console.log有效 - return doesn't work, console.log works NodeJS res.write无法正常工作 - NodeJS res.write doesn't work console works 我的 javascript 代码不起作用,但在控制台中执行时它起作用 - My javascript code doesn't work, but when it is executed in the console it works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM