【问题标题】:Problem with returned string with break tags带有中断标签的返回字符串的问题
【发布时间】:2011-08-18 21:33:16
【问题描述】:

问题:

By Rosa Golijan <br/><br/>The next time someone interrupts you to proclaim that "retweet"
or "woot" aren't actual words, laugh in his or her face and point to a copy of the 
Concise Oxford English Dictionary. <br/> <br/>Yes, one of the main authorities on the   
English language is officially acknowledging some of the silly terms we've been using 
lately. <br/> <br/>The twelfth edition of the Concise Oxford English Dictionary 
includes over 400 new words — many of which are related to social media and technology. 
According to the folks behind the esteemed reference text, these additions "are just 
carrying on the tradition of a dictionary that has always sought to be progressive and 
up to date."

问题:我很难找到一种简单的方法让我的动态 jquery 代码将 html 标记呈现为 html,而不是简单地将带有 html 中断标记的文本显示为文本的一部分。数据库/服务器端的文本最初包含换行符,然后我将这些新行转换为 (br/) 标记以进行 html 格式设置。现在由于文本以 json 格式返回,因此 html 标签按字面意思显示。

现在我使用 jquery .html() 和 .text() 进行测试,但 html 标记仍显示为文本的一部分。


感谢您的建议。当我有机会时,我会尝试一下。我正在使用 Salesforce,它有一些内置功能可以用它的专有组件处理这些问题,我只是想摆脱使用它们,因为它们不像我需要的那样动态。但是我能够强迫他们变得更有活力。

另外,JS Fiddle 是我以前从未听说过的东西。我一直随身携带两台笔记本电脑去工作,一台装有 Dreamweaver,一台是装有我所有其他必要软件的机器。没有dreamweaver 来测试jquery 成为一个问题,我不知道有一个基于云的工具可以让我做我在dreamweaver 中做的事情。非常感谢Chief先生的建议,这真的会派上用场!

还有什么其他工具可以让我使用吗?

【问题讨论】:

  • 您在哪里显示此文本?你能粘贴一些你的代码吗? jquery 的 .html() 使用浏览器的 innerHTML 属性,因此它应该可以正确呈现。

标签: jquery newline break carriage-return


【解决方案1】:

"...然后我将这些新行转换为 (br/) 标记以进行 html 格式设置。现在,由于文本以 json 格式返回,因此 html 标记按字面意思显示。"

我假设您的问题中有(br),因为您不确定如何使标签正确显示。

如果是这种情况,那么您可能正在使用.text() 添加新内容。

请改用.html()


如果您已经在使用.html(),请尝试一下:

$('.some_container').html( $('<div>').html(response_text).text() );

【讨论】:

  • 呜呜呜!!!谢谢@patrick-dw!!你的解决方案对我有用。我确实花了很多时间在这个问题上,时不时地重新审视它。对我来说是一个巨大的飞跃。这个: $('.some_container').html( $('
    ').html(response_text).text() );对我来说没有任何意义,但它有效!再次感谢!
  • @Antonio:不客气。我假设您的回复文本类似于&amp;lt;div&amp;gt; 而不是&lt;div&gt;。所以当你用.html() 插入它时,它只会把它变成实际的&lt; &gt; 字符。所以我们所做的是我们用$('&lt;div&gt;') 创建了一个新的div,使用.html() 将响应插入到其中,它会为您转换字符。然后我们使用.text() 获取那些现在转换的字符,最后使用.html() 将它们插入到页面上的实际容器中。
【解决方案2】:

您的字符串是否正确转义?我看到这对我有用:http://jsfiddle.net/mrchief/BekAx/1/

<div id=myDiv></div>


var str = 'By Rosa Golijan <br/><br/>The next time someone interrupts you to proclaim that "retweet" or "woot" aren\'t actual words, laugh in his or her face and point to a copy of the Concise Oxford English Dictionary. <br/> <br/>Yes, one of the main authorities on the English language is officially acknowledging some of the silly terms we\'ve been using lately. <br/> <br/>The twelfth edition of the Concise Oxford English Dictionary includes over 400 new words — many of which are related to social media and technology. According to the folks behind the esteemed reference text, these additions "are just carrying on the tradition of a dictionary that has always sought to be progressive and up to date."';

$('#myDiv').html(str);

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签