简体   繁体   English

jQuery和HTML忽略引号

[英]JQuery and Html ignore quotes

i'm programming in JS and html and having problem with displaying to the users strings which contains double quotes. 我正在用JS和html进行编程,无法向用户显示包含双引号的字符串。 I have strings, which can be edited by the users by adding free text. 我有字符串,用户可以通过添加自由文本来对其进行编辑。 I need to allow my uses to enter strings like = Hello "some text" bye. 我需要允许我的用途输入诸如= Hello“ some text” bye这样的字符串。 later this edited by user block of text is being saved to my DB. 稍后,由用户编辑的文本块将保存到我的数据库中。 After a quick check i found out that the string is being saved correctly. 快速检查后,我发现该字符串已正确保存。 later on i have to display to the users these block of text. 以后,我必须向用户显示这些文本块。 When i'm sending this list back (to client side) my object are correct, aka, containing all of the edited string (for example - Some text "other text"). 当我将该列表发回(到客户端)时,我的对象又正确了,又包含了所有已编辑的字符串(例如-某些文本为“其他文本”)。 Later i'm trying to display this text in html by pushing each line to the html: 稍后,我尝试通过将每一行推到html来在html中显示此文本:

htmlID.push('<input type=text value="'+MyString+'">')

but unfortunately i'm getting only the part of the string which is not inside the quotes, aka, Some text and "other text" is being skipped by html because of the quotes.( All the text inside the quotes in skipped and the quotes themselves as well. ) 但不幸的是,我只得到了引号内没有的字符串部分,也就是,由于引号,HTML跳过了一些文本和“其他文本”。(所有引号内的文本都被跳过了,并且引号自己也是。)

I think what i have to do is run over each string, check indexOf quotes and if so, replace them by other value like """ or """ but unfortunately i couldn't get any results. 我认为我需要对每个字符串运行,检查indexOf引号,如果是这样,请用其他值(例如“””或“””)替换它们,但不幸的是我无法获得任何结果。

i would be glad for some help, thank you 我很乐意提供帮助,谢谢

You need to replace all instances of " within the string with its HTML entity: &quot; : 您需要用其HTML实体替换字符串中的所有"实例: &quot;

 var MyString = 'Hello "some text" bye'.replace(/"/g, '&quot;'); $('#container').append('<input type="text" value="' + MyString + '">'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"></div> 

If you create a DOM element like below you wont have any problems with quotes. 如果您创建如下所示的DOM元素,则引号不会有任何问题。

var input = document.createElement('input');
input.setAttribute('type','text');
input.setAttribute('value',MyString);

htmlID.push(input.outerHTML);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM