【问题标题】:Can I apply remote: true to an html href link?我可以将 remote: true 应用于 html href 链接吗?
【发布时间】:2016-02-26 10:33:49
【问题描述】:

我需要一个完整的 div 作为链接,而我发现这样做的唯一方法是将 div 插入到纯 html href 链接中。

但我还需要远程链接。我猜了一下:

<a href="www.link.com" remote=true>
  <div id='link'></div>
</a>

虽然这种语法不会引发任何错误,但单击链接仍会打开一个新页面。

是否可以将 Rails 链接方法 remote: true 应用于 html href 链接?

【问题讨论】:

    标签: html ruby-on-rails ruby ajax


    【解决方案1】:

    这是我所说的“以错误的方式结合两种正确的方式做事”的示例。

    要么使用纯html 并编写javasript 脚本为div 提供onclick 函数,要么使用rubys 的link_to 并设置remote: true:这两种方法都可以正常工作给你

    但是将这两者结合起来是不行的,因为包含的remote: true 将无法解释。

    <%= link_to "www.link.com", remote: true do %>
      <div></div>
    <% end %>
    

    【讨论】:

    • 我将如何使用 ruby​​ 遥控器:在这种特殊情况下为真?
    • 我在上面的回答帖子中添加了一个代码示例
    【解决方案2】:

    根据http://guides.rubyonrails.org/working_with_javascript_in_rails.html

    您应该使用其中一个内置函数来生成远程 href &lt;%= link_to "an article", @article, remote: true %&gt; 与您的 &lt;a href="/articles/1" data-remote="true"&gt;an article&lt;/a&gt; 相比输出的 html 略有不同(注意远程是一个数据属性)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-09
      • 2023-01-15
      • 2011-10-02
      • 2021-03-23
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多