I want to click on this element.
<a href="/game.php?vil=9&screen=settings&action=sitter_login&player=305&h=417c13fb" target="_blank" class="btn">ini</a>
There are other elements, just like that where only the href changes. I can correctly find the element I want. But cannot click it... I tried the usual click() but didn't work:
document.getElementsByClassName("btn")[1].click()
Any help?
I tried this, it opens the same window, but I want to open in a new tab. I also put the internet option to open tabs with popups:
b=document.getElementsByClassName("btn")[1].href;
window.location.assign(b, '_blank');
How to open in new tab? I tried window.open() but never got to make it work:
window.open(b, '_blank');
答案 0 :(得分:0)
您可以在How can I trigger a JavaScript event click找到答案。如果执行.click()函数,则会触发在“click”(.onClick)上绑定的所有侦听器。 尝试侦听click事件,然后执行.click()函数
答案 1 :(得分:0)
我不确定我一直都理解这个问题。但是,当您实际单击某个链接时,所有其他JavaScripts都会停止加载并进入该网页。 window.open是对的。但您必须阻止Url实际将您重定向到网站。您可以通过对触发单击的eventobject使用preventDefault来执行此操作。希望这有帮助
let myLink = document.getElementsByClassName("btn")[1];
myLink.addEventListener('click', function newWindow(event) {
event.preventDefault();
window.open(this.href, '_blank')
})
myLink.click();