[英]Why button.click() is not working in my script?
I have a script in JavaScript that in a part of the code create a 'virtual button' and press it automatically.我在 JavaScript 中有一个脚本,在部分代码中创建一个“虚拟按钮”并自动按下它。
This button is not in the HTML page and is only used to store a link that at a certain point of the script must be clicked.此按钮不在 HTML 页面中,仅用于存储在脚本的某个点必须单击的链接。 Here's the code:
这是代码:
var virtualButton = document.createElement("button");
var linkText = document.createTextNode("assign to relevant user");
virtualButton.appendChild(linkText);
virtualButton.title = "assign to relevant user";
virtualButton.href = link;
document.body.appendChild(virtualButton);
virtualButton.click();
Everything seems fine to me, still the code does not actually click the button.对我来说一切都很好,但代码实际上并没有点击按钮。
Am I missing something here?我在这里错过了什么吗?
<button/>
elements do not have an .href
attribute. <button/>
元素没有.href
属性。 You most likely want to use an anchor ( <a/>
) element .您很可能希望使用锚点 (
<a/>
) 元素。 See below:见下文:
var virtualButton = document.createElement("a"); var linkText = document.createTextNode("assign to relevant user"); virtualButton.appendChild(linkText); virtualButton.title = "assign to relevant user"; virtualButton.href = "https://www.google.com"; document.body.appendChild(virtualButton); virtualButton.click();
(It won't actually navigate because it is in a sandboxed <iframe/>
, but you will see it attempt to do so). (它实际上不会导航,因为它位于沙盒
<iframe/>
中,但您会看到它尝试这样做)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.