【问题标题】:html_safe not working in link_to title property in railshtml_safe 在 rails 的 link_to 标题属性中不起作用
【发布时间】:2015-03-10 10:09:05
【问题描述】:

我想显示带有富文本的工具提示,如下所示

<%= link_to 'test', '#', :title => "<b>sample</b>".html_safe %>

它无法使用 html 标签显示。

希望在锚标题属性中显示没有 html 标记的 html 格式文本。

请分享你的想法。

【问题讨论】:

  • 你是如何显示标题的?
  • @RSB,你是什么意思?我想显示没有 html 标签的 html 格式的文本,因为它是 html_safe 会给我们没有标签的 html 格式的文本,但它不能在标题中工作-> 工具提示
  • 你不能把html(比如<b>标签)放在一个属性里面,包括title属性。如果您想要具有良好格式而不是标准标题的“丰富”弹出窗口,那么您将需要使用 javascript 来模仿 title 属性的效果。如果您使用的是像 Bootstrap 这样的“网络库”,那么它已经带有这个功能,称为“弹出框”。
  • @MaxWilliams, title -> 文本来自数据库,如:title => (@project.description).html_safe,但不是静态格式。由于描述字段将文本与格式的 html 标签混合。是的,正如你所说,我们需要使用 javascript 库,因为标准标题行不通。
  • 您的代码准确。如果您查看查询响应,您应该会看到 <a href="#" title="<b>sample</b>">test</a>。它是您的浏览器,它替换了 dom 属性中的 html 特殊字符。

标签: ruby-on-rails view tooltip html-safe


【解决方案1】:

最终实现如下,通过使用Jquery-ui工具提示

`

<%= link_to 'test', '#', :title => "<b>sample</b>", :id => "demo_box" %>`

    $(function(){
      $('#demo_box').tooltip({
        content: function(){
          var element = $( this );
          return element.attr('title')
        }
      });
    });

`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2014-09-10
    • 2011-05-27
    • 1970-01-01
    相关资源
    最近更新 更多