javascript 使用 jQuery.attr() 转义属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11591174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Escaping of attribute values using jQuery.attr()
提问by Artefact2
When using jQuery to update the title
attribute of an element, I find that the entities (notably
, which is the currently recommended way of adding line breaks in the title
attribute) do not display correctly (the entity shows up as text, and is not parsed…).
在使用 jQuery 更新title
元素的属性时,我发现实体(特别是
,这是当前推荐的在title
属性中添加换行符的方式)无法正确显示(实体显示为文本,并且没有被解析...... )。
For example:
例如:
<!-- This works fine -->
<div title="foo bar">baz</div>
But this:
但是这个:
<div>baz</div>
<script>
$("div").attr('title', 'foo bar'); // Escapes the &
</script>
Is there a way to not have jQuery escape the value? I tried using unescape()
before .attr()
, but it didn't work…
有没有办法不让 jQuery 转义值?unescape()
之前试过用.attr()
,没用。。。
回答by Esailija
jQuery isn't escaping anything, in fact your string is literally set as the title. You can simply use:
jQuery 不会转义任何内容,实际上您的字符串实际上已设置为标题。您可以简单地使用:
$("div").attr('title','foo\nbar')
There seems to be a whole lot of misunderstanding what escaping means. What you are doing is completely unnecessary.
似乎对转义的含义有很多误解。你在做什么是完全没有必要的。
Escaping is only necessary in a context where the character would otherwise have a special meaning and you want it not to have any special meaning.
仅在字符具有特殊含义且您希望它没有任何特殊含义的上下文中才需要转义。
For example, in the eyes of html parser, <
is special. You have to replace it with <
if you want to ensure it is treated as literal
<
. Emphasis on in the eyes of html parser. In javascript, <
is not special in a string in any way, thus you could
do elem.setAttribute("title", '""><b>hi</b>')
and that's what you get, the element's title will literally be ""><b>hi</b>
.
比如在html解析器的眼里,<
就是特殊的。<
如果您想确保将其视为文字,则必须将其替换为
<
。重视在html 解析器的眼中。在 javascript 中,<
字符串在任何方面都不是特殊的,因此你可以这样做elem.setAttribute("title", '""><b>hi</b>')
,这就是你得到的,元素的标题实际上是""><b>hi</b>
.
As for html code point references, they are generally useless. You can simply use literal characters. Instead of writing ♥
, you can simply
write ?
. Instead of writing ö
, you can simply write ?
. Here's http://jsfiddle.net/fnqDn/2/.
至于html代码点引用,一般没什么用。您可以简单地使用文字字符。♥
您可以简单地写而不是写?
。ö
您可以简单地写而不是写?
。这是http://jsfiddle.net/fnqDn/2/。
回答by spinningarrow
You are right; unescape()
will not work in this case. What you can do is something similar to what is mentioned in the answer here.
你说的对; unescape()
在这种情况下将不起作用。您可以做的类似于此处的答案中提到的内容。
Basically, create an empty element using jQuery, set it's HTML to the text you want in the title ('foo bar'
in this case) and then get back the element's text.
基本上,使用 jQuery 创建一个空元素,将它的 HTML 设置为标题中所需的文本('foo bar'
在本例中),然后取回元素的文本。
Sounds a little complicated? It is, but this will work for any HTML entity and it's really just a one-liner. Here's the code:
听起来有点复杂?是的,但这适用于任何 HTML 实体,而且它实际上只是一个单行。这是代码:
<div>baz</div>
<script>
var title = $('<div/>').html('foo bar').text();
$("div").attr('title', title);
</script>
Or on one line:
或者在一行上:
$('div').attr('title', $('<div/>').html('foo bar').text());
回答by pat34515
This function will save you time, if you'll be doing this a lot:
如果您经常这样做,此功能将节省您的时间:
function unescp(x){
return $('<div/>').html(x).text();
}
$("div").attr('title',unescp('foo bar'));
See it here: http://jsfiddle.net/9Lnru/?
在这里看到它:http: //jsfiddle.net/9Lnru/?
回答by Andreas Nilsson
You could try to put the value in a hidden element. And let jquery copy that value to the div. Im not sure if it would pass through jQuery unnoticed, but it's worth a try...
您可以尝试将该值放在隐藏元素中。并让 jquery 将该值复制到 div。我不确定它是否会在不被注意的情况下通过 jQuery,但值得一试......