Consider the tricks that <a href> knows by default but javascript linking won't do for you. On a decent website, anything that wants to behave as a link should implement these features one way or another. Namely:
Ctrl+Click: opens link in new tab You can simulate this by using a window.open() with no position/size argument
Shift+Click: opens link in new window You can simulate this by window.open() with size and/or position specified
Alt+Click: download target People rarely use this one, but if you insist to simulate it, you'll need to write a special script on server side that responds with the proper download headers.
EASY WAY OUT
Now if you don't want to simulate all that behaviour, I suggest to use <a href> and style it like a button, since the button itself is roughly a shape and a hover effect. I think if it's not semantically important to only have "the button and nothing else", <a href> is the way of the samurai. And if you worry about semantics and readability, you can also replace the button element when your document is ready(). It's clear and safe.
Well, for a link, there must be a link tag around. what you can also do is that make a css class for the button and assign that class to the link tag. like,
You can make it a non-submitting button (<button type="button">) and hook something like window.location = 'http://where.you.want/to/go' into its onclick handler. This does not work without javascript enabled though.
Or you can make it a submit button, and do a redirect on the server, although this obviously requires some kind of server-side logic, but the upside is that is doesn't require javascript.
(actually, forget the second solution - if you can't use a form, the submit button is out)
发布评论
评论(10)
内联 Javascript:
在 Javascript 中定义函数:
或在 Jquery 中
Inline Javascript:
Defining a function in Javascript:
or in Jquery
这是一个即使在禁用 JavaScript 时也能工作的解决方案:
技巧是用按钮自己的
我个人更喜欢
标签,但您也可以使用
来实现:
Here's a solution which will work even when JavaScript is disabled:
The trick is to surround the button with its own
<form>
tag.I personally prefer the
<button>
tag, but you can do it with<input>
as well:就这么做吧
虽然我已经有一段时间没有接触 JavaScript 了 - 也许
location.href
已经过时了?无论如何,我就是这么做的。Just do this
Although, it's been a while since I've touched JavaScript - maybe
location.href
is outdated? Anyways, that's how I would do it.链接很棘手
考虑一下 的技巧。默认情况下知道,但 javascript 链接对你不起作用。在一个像样的网站上,任何想要充当链接的东西都应该以某种方式实现这些功能。即:
您可以使用不带位置/大小参数的 window.open() 来模拟此操作
您可以通过指定大小和/或位置的 window.open() 来模拟它
人们很少使用这个,但如果您如果坚持模拟它,您需要在服务器端编写一个特殊的脚本,以正确的下载标头进行响应。
简单的方法
现在,如果您不想模拟所有这些行为,我建议使用 并将其样式设置为按钮,因为按钮本身大致是一个形状和悬停效果。我认为如果只有“按钮而没有其他”在语义上并不重要,那么 就可以了。是武士之道。如果您担心语义和可读性,您还可以在文档准备好()时替换按钮元素。既清晰又安全。
LINKS ARE TRICKY
Consider the tricks that <a href> knows by default but javascript linking won't do for you. On a decent website, anything that wants to behave as a link should implement these features one way or another. Namely:
You can simulate this by using a window.open() with no position/size argument
You can simulate this by window.open() with size and/or position specified
People rarely use this one, but if you insist to simulate it, you'll need to write a special script on server side that responds with the proper download headers.
EASY WAY OUT
Now if you don't want to simulate all that behaviour, I suggest to use <a href> and style it like a button, since the button itself is roughly a shape and a hover effect. I think if it's not semantically important to only have "the button and nothing else", <a href> is the way of the samurai. And if you worry about semantics and readability, you can also replace the button element when your document is ready(). It's clear and safe.
那么,对于链接来说,周围必须有一个链接标签。您还可以做的是为按钮创建一个 css 类并将该类分配给链接标签。喜欢,
Well, for a link, there must be a link tag around. what you can also do is that make a css class for the button and assign that class to the link tag. like,
您可以将其设为非提交按钮 (
) 并挂钩类似
window.location = 'http://where.you.want/ to/go'
进入其 onclick 处理程序。如果没有启用 JavaScript,这将不起作用。或者您可以将其设为提交按钮,并在服务器上进行重定向,虽然这显然需要某种服务器端逻辑,但好处是不需要 JavaScript。
(实际上,忘记第二个解决方案 - 如果您不能使用表单,则提交按钮将消失)
You can make it a non-submitting button (
<button type="button">
) and hook something likewindow.location = 'http://where.you.want/to/go'
into its onclick handler. This does not work without javascript enabled though.Or you can make it a submit button, and do a redirect on the server, although this obviously requires some kind of server-side logic, but the upside is that is doesn't require javascript.
(actually, forget the second solution - if you can't use a form, the submit button is out)
我刚刚弄清楚了这一点,它完美地链接到另一个页面,而无需我的默认链接设置覆盖我的按钮类! :)
I just figured this out, and it links perfectly to another page without having my default link settings over ride my button classes! :)
这里使用的是 jQuery。请访问 http://jsfiddle.net/sQnSZ/ 查看其实际情况
Here it is using jQuery. See it in action at http://jsfiddle.net/sQnSZ/
假设在您的 HTML 文件中您有一个 id="Button" 的按钮,在 script.js(您的脚本文件)中,您可以使用以下方式:
现在按钮将引导您到 Google!
欲了解更多信息:https://www.w3schools.com/js/js_window_location.asp
Assuming that in your HTML file you've a button with id="Button", In the script.js(your script file), you can use this way:
Now the button will lead you to Google!
For more info: https://www.w3schools.com/js/js_window_location.asp
您也可以尝试这个#
You can also try this
<button type=“Submit”><a href=“”>#</a></button>