[英]How to Redirect after Click on Button
我正在使用着陆页,所以我已经有了按钮我想在点击这个按钮后重定向人们按钮 ID 是:button-c86b272c 我想在点击这个按钮后重定向人们而不是创建新按钮。
我有这段代码在超时后重定向:
<meta http-equiv="refresh" content="10; URL=www.google.com/" />
但我希望在点击令人兴奋的按钮而不是新按钮后重定向我尝试将第一个代码更改为此:
<meta http-equiv="refresh" onclick="button-c86b272c; URL=www.google.com/" />
但它没有用 有些地方不对 希望你能帮助我
meta
标记不是可见的 DOM 元素,这意味着它没有“主体”,因此无法点击。 meta
标签用于描述有关您页面的元数据,或设置 HTTP 标头。
要在单击按钮后重定向到特定 URL,您可以使用 JavaScript:
// First we need to create a handle to the DOM element
const buttonElement = document.getElementById("button-c86b272c");
// Then we add an event listener, which will execute a passed function after the event (in our case "click") is fired
buttonElement.addEventListener("click", () => {
// We set the destination URL, the page reloads automatically
window.locaton = "www.google.com/";
});
将此代码添加到您的页面或将其放在单独的文件中并确保将其导入,或者您可以使用onclick
属性直接在 HTML 中添加事件侦听器,如下所示:
<button id="button-c86b272c" onclick="window.location = 'www.google.com/'"></button>
但是,不推荐使用这种方法,最好将 JavaScript 放在单独的文件中,不要将其混入 HTML 中。
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.