简体   繁体   English

jQuery URI编码(char&).html()属性值

[英]jQuery URI encode (char &) .html() attribute value

I've read a lot of the HTML encoding post for the last day to solve this. 为了解决这个问题,我在最后一天阅读了很多HTML编码文章。 I just managed to locate it. 我只是设法找到它。 Basicly I have set an attribute on an embed tag with jQuery. 基本上,我已经在jQuery的embed标签上设置了一个属性。 It all works fine in the browser. 在浏览器中一切正常。 No I want to read the HTML itself to add the result as a value for an input field to let the user copy & past it. 不,我想阅读HTML本身以将结果添加为输入字段的值,以使用户复制并粘贴它。

The PROBLEM is that the .html() function (also plain JS .innerHTML) converts the '&' char into '& amp;' 问题是.html()函数(也是普通的JS .innerHTML)将'&'char转换为'&' (without the space) . (没有空格) Using differen html encoder functions doesnt make a difference. 使用不同的html编码器功能没有任何区别。 I need the '&' char in the embed code. 我需要在嵌入代码中使用'&'char。
Here is the code: 这是代码:

HTML: HTML:

<div id="preview_small">
<object><embed src="main.swf?XY=xyz&YXX=xyzz"></embed>
</object></div>

jQuery: jQuery的:

$("#preview_small object").clone().html();

returns 退货

... src=main.swf?XY=xyz&amp;YXX=xyzz ...

When I use: 当我使用时:

$("#preview_small object").clone().children("embed").attr("src");

returns 退货

main.swf?XY=xyz&YXX=xyzz

Any ideas how I can get the '&' char direct, without using regex after I got the string with .html() 任何获得.html()字符串后如何直接使用'&'char的想法,而无需使用正则表达式

I need the & char in the embed code. 我需要在嵌入代码中添加& char。

No you don't. 不,你不会。 This: 这个:

<embed src="xyz&YXX=xyz"></embed>

is invalid HTML. 是无效的HTML。 It'll work in browsers since they try to fix up mistakes like this, but only as long as the string YXX doesn't happen to match an HTML entity name. 它会在浏览器中起作用,因为它们会尝试修复此类错误,但YXX是只要字符串YXX与HTML实体名称不匹配即可。 You don't want to rely on that. 您不想依靠它。

This: 这个:

<embed src="xyz&amp;YXX=xyz"></embed>

is correct, works everywhere, and is the version you should be telling your users to copy and paste. 是正确的,可以在任何地方使用,并且是您应该告诉用户复制和粘贴的版本。

attr("src") returns xyz&YXX=xyz attr("src")返回xyz&YXX=xyz

Yes, that's the underlying value of that attribute. 是的,那是该属性的基础值。 Attribute values and text content can contain almost any character directly. 属性值和文本内容可以直接包含几乎任何字符。 It's only the HTML serialisation of them where they have to be encoded: 只是必须对其进行编码的HTML序列化:

<div title="a&lt;b&quot;&amp;c&gt;d">

$('div').attr('title') -> a<b"&c>d

I want to read the HTML itself to add the result as a value for an input field 我想阅读HTML本身,将结果添加为输入字段的值

<textarea id="foo"></textarea>

$('#foo').val($('#preview_small object').html());

However note that the serialised output of innerHTML / html() is not in any particular fixed dialect of HTML, and in particular IE may give you code that, though generally understandable by browsers, is also not technically valid: 但是请注意, innerHTML / html()的序列化输出不是在HTML的任何特定固定方言中,尤其是IE可能会为您提供虽然在浏览器中通常可以理解的代码,但在技术上还是无效的:

$('#somediv').html('<div title="a/b"></div>');
$('#somediv').html() -> '<DIV title=a/b></DIV>' - missing quotes

So if you know the particular format of HTML you want to present to the user, you may be better off generating it yourself: 因此,如果您知道要呈现给用户的HTML特定格式,则最好自己生成:

function encodeHTML(s) {
    return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
}

var src= 'XY=xyz&YXX=xyzz';
$('#foo').val('<embed src="'+encodeHTML(src)+'"><\/embed>');

(The \\/ in the close tag is just so that doesn't get mistaken as the end of a <script> block, in case you're in one.) (在close标记中的\\/只是为了避免误认为<script>块的结尾,以防万一。)

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

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