如何使用 javascript 将 click() 应用于标签
我正在尝试模拟单击一个没有 id 或类的基本链接。
<a href="http://www.myebsite.com/service/playnow">Click to Start</a>
我有以下代码,但是当我加载页面时,不执行任何单击操作。我做错了什么?
var a = document.evaluate( '//a[contains(@href, "playnow")]' ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
if(a){
a.click();
}
I am trying to emulate a click on a basic link that appears like this with no id or class.
<a href="http://www.myebsite.com/service/playnow">Click to Start</a>
I have the following code but when i load the page to no click action is performed. What am I doing wrong?
var a = document.evaluate( '//a[contains(@href, "playnow")]' ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
if(a){
a.click();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是向锚标记添加 id,然后在 DOM 元素上调用
click
函数HTML:
JavaScript:
The easiest way is to add an id to the anchor tag and then invoke the
click
function on the DOM elementHTML:
JavaScript:
为什么你的标签没有 ID 很奇怪,但我假设由于某种原因你无法控制它。我认为您无法通过“单击”方法来模拟单击,因此也许可以尝试以下操作:
Why your tag doesn't have an ID is strange, but I'll assume for some reason you can't control that. I don't think you can emulate a click through a "click" method, so perhaps try something like: