[英].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.