Rails中的HTML标题属性只显示第一个单词 [英] HTML title attribute in Rails displaying only first word

查看:102
本文介绍了Rails中的HTML标题属性只显示第一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个奇怪的小问题。



我想在项目上创建文本悬停以显示该项目的描述,该描述是该项目模型的属性。所以我在我看来基本上做到了这一点:

 < div title =<%= item.description%> ><%= item.name%>< / div> 

奇怪的是,所有模型调用都正常工作(item.name和item.description正在调用正确的东西),当我悬停时,只有item.description的第一个字出现。换句话说,如果item.description是这是一个超酷的项目!,当我将鼠标悬停在item.name上时,悬停只是说This。



这可能与描述属性有关,该属性当前是文本类型的(我认为它适用于长字符串,就像我的描述一样)。但也许不是。



任何想法为什么会发生这种情况,以及如何解决它?

解决方案

我认为你需要在视图中为HTML添加引用。如果您在生成的HTML中查看源代码,我愿意打赌它看起来像这样:

 < div title = This是一个超级酷的项目! > 

浏览器会将其解释为 div 有一个标题,其值 This ,然后名为的属性为 a super cool item!



如果您将视图更改为此:

 < div title =<%= item.description%> > 

然后您生成的HTML应该如下所示:

 < div title =这是一件超酷的物品! > 


Got a weird little problem.

I want to create the text hover on an item to display that item's "description," which is an attribute of that item's model. So I basically did this in my view:

<div title=<%= item.description %> ><%= item.name %></div>

The weird thing is, while all the model calls are working correctly (item.name and item.description are calling up the right things), only the first word of item.description is showing up when I hover. In other words, if item.description is "This is a super cool item!", when I hover over that div with item.name, the hover just says "This".

This may have something to do with the :description attribute, which is currently of type text, (which I thought was for long strings, like my descriptions). But maybe not.

Any idea why this might be happening, and how to fix it?

解决方案

I think you need to add quoting to the HTML in the view. If you view source on the generated HTML, I'm willing to bet it looks like this:

<div title=This is a super cool item! >

The browser will interpret that as the div having a title with the value This, and then attributes named is, a, super, cool, and item!.

If you change your view to this:

<div title="<%= item.description %>" >

Then your generated HTML should look like this:

<div title="This is a super cool item!" >

这篇关于Rails中的HTML标题属性只显示第一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆