简体   繁体   English

使JQuery .html()将属性值包装在单引号中

[英]Making JQuery .html() wrap attribute values in single quotes

JQuery by defaults wraps attribute values in double quotes, it also overrides even if the original html string had single quotes as in this example: 默认情况下,JQuery将属性值用双引号引起来,即使原始html字符串具有单引号,它也会覆盖此示例中的示例:

$("<div><span class= 'foo' id='foo2' >abc</span></div>").html();

Output: 输出:

<span class="foo" id="foo2">abc</span>

I looked up in JQuery documentation, also searched but couldn't find any thing related to this, while I can write a regex replace such as this but I don't want to manipulate HTML with regex for obvious reasons, I was wondering if there is a profound way of achieving this . 我jQuery文档中抬起头来,也搜查,但找不到与此相关的任何事情,而我可以写一个正则表达式替换,例如这个 ,但我不想操纵与正则表达式的原因是显而易见的HTML, 我想知道是否有是实现这一目标的深刻途径

Edit: 编辑:

The reason it is bothering me is at some places in the application there exists codes like htmlstring.indexOf("attr='value'") which eventually broke when I did some manipulation in the htmlstring by converting it into JQuery object and back to string. 困扰我的原因是在应用程序中的某些地方,存在诸如htmlstring.indexOf("attr='value'") ,当我通过将其转换为JQuery对象并返回字符串的方式对htmlstring进行某些操作时,最终破裂。

Edit 2: 编辑2:

I do not need a solution for the particular problem I stated in my upper edit, I already handled that, what made me ask this question was, is it possible or what is the better way of changing the quote wrapping attributes in HTML. 我不需要在上层编辑中提到的特定问题的解决方案,我已经处理了这个问题,是什么使我问这个问题,是可能的还是改变HTML中的引号环绕属性的更好方法?

We have the choice of using single or double quotes for the attributes in HTML but… 我们可以选择对HTML中的属性使用单引号或双引号,但是…
… when the page is rendered, they all become double quotes. …呈现页面时,它们都变成双引号。

I suggest you to use the .attr() method instead of using .indexOf() . 我建议您使用.attr()方法,而不要使用.indexOf()

See the snippet below: 请参见下面的代码段:

 var htmlstring1 = document.getElementById('myDiv1').outerHTML; var htmlstring2 = document.getElementById('myDiv2').outerHTML; console.log("string:", htmlstring1); console.log("string:", htmlstring2, '\\n\\n'); // Note that attr='value' of myDiv2 becomes attr="value" in console console.log(`Using .indexOf('attr="value"'): (will always work)`) if (htmlstring1.indexOf('attr="value"') > -1) console.log('Found.'); else console.log('Not found.'); console.log(`Using .indexOf("attr='value'"): (will never work)`) if (htmlstring1.indexOf("attr='value'") > -1) console.log('Found.'); else console.log('Not found.'); console.log(`Using .attr('attr') == 'value'): (a suggestion)`) if ($('#myDiv1').attr('attr') == 'value') console.log('Found.'); else console.log('Not found.'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="myDiv1" attr="value"></div> <div id="myDiv2" attr='value'></div> 

⋅ ⋅ ⋅ ⋅⋅⋅

After your comment, I tried some few things… 发表您的评论后,我尝试了一些尝试…

As you've talked about in your question, there's a trick here: 正如您在问题中所讨论的,这里有一个窍门:
When an element string is used by jQuery, it gets normalized with double quotes: jQuery使用元素字符串时,会使用双引号将其标准化:

 var htmlstring = "<span class='foo' id='foo2'>abc</span>"; console.log(htmlstring); var htmlparsed = $(htmlstring)[0]; console.log(htmlparsed); // You could even create a function to handle the normalization of quotes function norm_quotes(str){ return $(str)[0]; } console.log(norm_quotes(htmlstring)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

I'd suggest you to use your .indexOf("attr='value'") on a string only after it has been parsed by jQuery. 我建议您仅在 jQuery解析了字符串之后.indexOf("attr='value'")字符串使用.indexOf("attr='value'")

Hope it helps. 希望能帮助到你。

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

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