Javascript 在html中点击href

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9046003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:28:41  来源:igfitidea点击:

Onclick on href in html

javascripthtmlonclick

提问by Itzik984

I want to make a button that will redirect me to another page.

我想做一个按钮,将我重定向到另一个页面。

the code:

编码:

 <div align="right" ><input type="submit" id="logout" onclick="location.href=/login?dis=yes" value="Sign Out" ></div><BR>

but when i press the button, i dont get any redirection to the other page.

但是当我按下按钮时,我没有重定向到其他页面。

any idea why?

知道为什么吗?

回答by Rick Kuipers

Perhaps try this using a type="button" and put quotes around the location.

也许尝试使用 type="button" 并在该位置周围加上引号。

<div align="right"><input type="button" id="logout" onclick="location.href='/login?dis=yes'" value="Sign Out" ></div>

回答by Jukka K. Korpela

Your current code, even if the syntax is fixed by adding quotation marks, creates a button that does not work when JavaScript is disabled. The simpler and safer approach is a small HTML form:

您当前的代码,即使通过添加引号来修复语法,也会创建一个在禁用 JavaScript 时不起作用的按钮。更简单、更安全的方法是一个小的 HTML 表单:

<form action="/login">
<input type=hidden name=dis value=yes>
<input type=submit value="Sign Out">
</form>

To achieve the desired styling, you could add the following stylesheet:

要实现所需的样式,您可以添加以下样式表:

form { display: inline; float: right; }

回答by Rey Rahadian

don't use

不要使用

<input type="submit" />

because this is used to post the form, instead you should use

因为这是用来发布表单的,​​所以你应该使用

<input type="button" onclick='location.href="/login?dis=yes"'/> 

for redirection or plain anchor tag

用于重定向或普通锚标记

<a href="/login?dis=yes">Sign Out</a>

回答by Kyle Gagnon

I would tell you the same thing everyone else has. Instead of using: location.herfyou should use: window.locationHope this helps you!

我会告诉你其他人都有的同样的事情。而不是使用: location.herf你应该使用: window.location希望这对你有帮助!

回答by Mujassir Nasir

try this

尝试这个

window.location='/login?dis=yes';

as

作为

        <div align="right" >
<input type="submit" id="logout" 
    onclick="window.location='/login?dis=yes';" value="Sign Out" ></div>

回答by robert

Your javascript code is invalid.

您的 JavaScript 代码无效。

Use this:

用这个:

<div align="right" ><input type="submit" id="logout" onclick="location.href='/login?dis=yes'" value="Sign Out" ></div><BR>

回答by OpenR

  • Set the target to Window.Location so you get the right target changed.
  • add return false; so that the submit button does not try to submit the form.
  • Put the URL in single quotes to avoid parsing errors.
  • 将目标设置为 Window.Location,以便更改正确的目标。
  • 添加返回假;这样提交按钮就不会尝试提交表单。
  • 将 URL 放在单引号中以避免解析错误。

<input type="submit" id="logout" onclick="window.location.href='/login?dis=yes';return false;" value="Sign Out">

<input type="submit" id="logout" onclick="window.location.href='/login?dis=yes';return false;" 值="退出">

The main reason that your button, as designed, will not work is because it is a "Submit" button. When you click on this it is trying to do the submit action after running your onclick action, essentially ignoring the onclick action.

您的按钮按设计不起作用的主要原因是因为它是“提交”按钮。当您点击它时,它会在运行您的 onclick 操作后尝试执行提交操作,基本上忽略了 onclick 操作。

You can fix this by using a type="button" button, or the above solution I listed if you must use a "submit" button.

您可以使用 type="button" 按钮解决此问题,或者如果您必须使用“提交”按钮,则可以使用我列出的上述解决方案。