Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何自动单击div中的链接_Javascript_Dom - Fatal编程技术网

Javascript 如何自动单击div中的链接

Javascript 如何自动单击div中的链接,javascript,dom,Javascript,Dom,我试图在短暂延迟后自动单击div中的第一个链接,但我的代码不起作用。这里怎么了 //HTML <div id="main"> <p>The first paragraph.</p> <a href="https://www.google.com" target="_blank">Google link</a> <p>The second paragraph.</p> </div> //Just ch

我试图在短暂延迟后自动单击div中的第一个链接,但我的代码不起作用。这里怎么了

//HTML
<div id="main">
<p>The first paragraph.</p>
<a href="https://www.google.com" target="_blank">Google link</a>
<p>The second paragraph.</p>
</div>

//Just checking that I selected the link
<p id="demo"></p>

<script>
var x = document.getElementById("main");
var y = x.getElementsByTagName("a");

//Here's just checking I got the link
document.getElementById("demo").innerHTML = 
'The first link (index 0) inside "main" is: ' + y[0].href;

//Here's the timer
    window.setTimeout("autoClick()", 2000);

//And this is what isn't working...
function autoClick() {
var linkPage = y[0];
window.location.href = linkPage;
}
</script>
//HTML
第一段

第二段

//只是检查我是否选择了链接

var x=document.getElementById(“main”); 变量y=x.getElementsByTagName(“a”); //这里只是检查一下我有链接 document.getElementById(“demo”).innerHTML= 'main'中的第一个链接(索引0)是:'+y[0]。href; //这是计时器 setTimeout(“autoClick()”,2000); //这是不起作用的。。。 函数autoClick(){ var linkPage=y[0]; window.location.href=linkPage; }
很明显,我遗漏了一些非常明显的东西,因为自动单击不起作用,但在查找时看不到它-有人能看到我犯了什么基本错误吗

window.location.href = linkPage.getAttribute('href')


您需要使用
getAttribute('href')
来检索href值。你的台词应该是:

window.location.href = linkPage.getAttribute('href');
您的完整代码应该如下所示(格式化并稍加改进):


第一段

第二段

设x=document.getElementById(“main”); 设y=x.getElementsByTagName(“a”); //测试代码。。。 document.getElementById(“demo”).innerHTML='main中的第一个链接(索引0)是:'+y[0]。href; //2秒后自动单击。。。 设置超时(自动单击,2000); 函数autoClick(){ window.location.href=y[0].getAttribute('href'); }
笔记
  • 使用代替
  • 调用
    autoClick
    函数时不需要括号或引号
  • 无需创建变量
    linkPage
    ,只需使用
    y[0]

您是否已确保
y
在范围内且在功能内具有价值。。看来你超出了范围
y
x
应该在函数中设置。谢谢-已经排序好了@JackSamson-如果答案能解决您的问题,请接受:-)欢迎。如果你把答案记下来并投赞成票,那就太好了
window.location.href = linkPage.getAttribute('href');
<div id="main">
    <p>The first paragraph.</p>
    <a href="https://www.google.com" target="_blank">Google link</a>
    <p>The second paragraph.</p>
</div>

<p id="demo"></p>

<script>

    let x = document.getElementById("main");
    let y = x.getElementsByTagName("a");

    // Test code...
    document.getElementById("demo").innerHTML = 'The first link (index 0) inside "main" is: ' + y[0].href;

    // Auto click after 2 seconds...
    window.setTimeout(autoClick, 2000);

    function autoClick() {
        window.location.href = y[0].getAttribute('href');
    }

</script>