简体   繁体   English

为什么我的 javascript 重定向不起作用“”?

[英]Why does my javascript redirect not work “”?

Why doesn't the redirectFunction() function redirect to another link?为什么redirectFunction()函数不重定向到另一个链接?

 document.querySelectorAll(".redirectBtn") .addEventListener("click", redirectFunction); function redirectFunction() { window.location.href = ("https://stackoverflow.com/questions/29475323/call-function-with-classname-onclick-js/29475365"); }
 <h1>Redirect to a Webpage Example</h1> <button class="redirectBtn">Redirect</button> <h2>Click the above button to Redirect to another Webpage</h2>

You need to use querySelector instead of querySelectorAll .您需要使用querySelector而不是querySelectorAll querySelectorAll returns a list of dom elements on which you can't invoke the addEventListener method. querySelectorAll返回一个 dom 元素列表,您不能在这些元素上调用addEventListener方法。

 document.querySelector(".redirectBtn") .addEventListener("click", redirectFunction); function redirectFunction() { window.location.href = ("https://stackoverflow.com/questions/29475323/call-function-with-classname-onclick-js/29475365"); }
 <h1>Redirect to a Webpage Example</h1> <button class="redirectBtn">Redirect</button> <h2>Click the above button to Redirect to another Webpage</h2>

Note: querySelectorAll always return an array.注意: querySelectorAll总是返回一个数组。 You cannot add event listener to an array.您不能向数组添加事件侦听器。 Loop through the nodes and add event listener on individual node.循环遍历节点并在单个节点上添加事件侦听器。

 document.querySelectorAll(".redirectBtn").forEach((node) => node.addEventListener("click", redirectFunction)) function redirectFunction() { window.location.href = ("https://stackoverflow.com/questions/29475323/call-function-with-classname-onclick-js/29475365"); }
 <h1>Redirect to a Webpage Example</h1> <button class="redirectBtn">Redirect</button> <h2>Click the above button to Redirect to another Webpage</h2>

querySelectorAll返回一个节点集合,您必须使用循环添加eventListener或者您可以像这样使用querySelector

document.querySelector(".redirectBtn").addEventListener("click", redirectFunction);

Because querySelectorAll returns an array of elements that matches the query.因为querySelectorAll返回与查询匹配的元素数组。 Since you only have one element, with that query, you need to use querySelector , which only selects one (1st) element.由于您只有一个元素,因此对于该查询,您需要使用querySelector ,它只选择一个(第一个)元素。

 document.querySelector(".redirectBtn") .addEventListener("click", redirectFunction); function redirectFunction() { window.location.href = ("https://stackoverflow.com/questions/29475323/call-function-with-classname-onclick-js/29475365"); }
 <h1>Redirect to a Webpage Example</h1> <button class="redirectBtn">Redirect</button> <h2>Click the above button to Redirect to another Webpage</h2>

there is a space here:这里有一个空格:

you have to remove it你必须删除它

document.querySelector(".redirectBtn")XXXXXXXXXXXXXXXXXXXXX.addEventListener("click", redirectFunction);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM