如何使用 addEventListener 在按钮点击时重定向到另一个 HTML 页面?

huangapple go评论60阅读模式
英文:

How to use addEventListener to redirect to another html page on button click?

问题

如何使用addEventListner重定向到下一个页面?

在这里,当我点击按钮时,它必须重定向到另一个HTML页面
如何编写代码实现这一点。

HTML

<button>提交</button>

JS

let btn = document.querySelector('button');
btn.addEventListener('click', () => {
    // 在这里添加重定向到下一个页面的代码
})
英文:

How to redirect to the next page using addEventListner?

Here, when I click the button it has to redirect to another HTML page
how to write the code for that.

HTML

&lt;button&gt;submit&lt;/button&gt;

JS

let btn = document.querySelector(&#39;button&#39;)
btn.addEventListener(&#39;click&#39;, () =&gt; {
    
})

答案1

得分: 1

你可以尝试这样做:

let btn = document.querySelector('button')
btn.addEventListener('click', () => {
    window.location.replace('http://www.example.com');
})
英文:

You can try this:

let btn = document.querySelector(&#39;button&#39;)
btn.addEventListener(&#39;click&#39;, () =&gt; {
    window.location.replace(&#39;http://www.example.com&#39;);
})

huangapple
  • 本文由 发表于 2023年5月25日 20:00:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定