Ruby-on-rails Rails:使用link_to 建立一个没有href 的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12081156/
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
Rails: using link_to to make a link without href
提问by bevanb
How can I use the link_tohelper to make a link without an hrefattribute? I want to make a link exclusively for a javascript action, I don't want it reloading the page or anything.
如何使用link_to助手创建没有href属性的链接?我想为一个 javascript 操作专门创建一个链接,我不希望它重新加载页面或任何东西。
When I omit the url parameter, clicking the link reloads the page. Ditto for providing nilin place of the url.
当我省略 url 参数时,单击链接会重新加载页面。同样用于提供nil代替 url。
回答by niels
回答by Cody Caughlan
I use this approach:
我使用这种方法:
= link_to('Click me', 'javascript:;', :id => :foo)
Its basically a Javascript no-op
它基本上是一个 Javascript no-op
回答by Solomon
Set the href to:
将 href 设置为:
javascript:void(0)
This will let you have a link that clicking it does nothing, doesn't reload the page, and doesn't move the page up to the top.
这将使您拥有一个链接,单击它不会执行任何操作,不会重新加载页面,也不会将页面移至顶部。
回答by stephenmurdoch
I think you're better off using a button in situations where there is no href.
我认为在没有 href 的情况下最好使用按钮。
<button class='button'>Hello</button>
回答by Andrey
<%= link_to '+ Add Make', 'javascript:void(0)', id: 'add-make', onclick: 'addMake()' %>
and then your jQuery:
然后你的jQuery:
function addMake() {
$.ajax({
url: '/dealers',
type: 'GET',
dataType: 'script',
success: function (data) {
console.log('it was successful')
$('.add-make').hide();
},
error: function (data) {
console.log('it was error')
}
});
}
回答by Aaron Gray
An alternative is
另一种选择是
= link_to("My hyperlink", "#")
This will give you a link that when clicked, does not reload the page like passing in nil. At the same time, it will set the mouse to pointer on mouseover, which you will not get with the content_tag approach.
这将为您提供一个链接,单击该链接时,不会像传入 nil 那样重新加载页面。同时,它会将鼠标设置为鼠标悬停时的指针,这是使用 content_tag 方法无法实现的。
回答by tordoch
An example for rails 5 would be:
rails 5 的一个例子是:
<%= content_tag("a", image_tag("/logo.png"), class: "navbar-brand") %>
<%= content_tag("a", image_tag("/logo.png"), class: "navbar-brand") %>
This would render as:
这将呈现为:
<a class="navbar-brand"><img src="/logo.png" alt="Logo"></a>
<a class="navbar-brand"><img src="/logo.png" alt="Logo"></a>
This is similar to niels' answer but with a nested image_tagand a class.
这类似于niels的答案,但具有嵌套image_tag和类。
Source: Rails API
来源:Rails API
回答by Artur Beljajev
I prefer using plain HTML without hrefattribute at all in such cases:
href在这种情况下,我更喜欢使用没有属性的纯 HTML :
<a data-toggle="modal" data-target="#myModal">Open dialog</a>
IMHO it is more readable than content_tag("a","link text").
恕我直言,它比content_tag("a","link text").
- The disadvantage of
href=#is that the browser will scroll to the top when clicked by default javascript:;looks ugly
- 缺点
href=#是浏览器默认点击会滚动到顶部 javascript:;看起来很丑

