简体   繁体   English

如果有样式标签,则jQuery的.html函数无法将内容正确写入textarea

[英]jQuery's .html function doesn't write content to textarea correctly if there is a style tag

Here's the basic setup: We're adding some HTML inside textareas using JavaScript/jQuery. 这是基本设置:我们正在使用JavaScript / jQuery在文本区域内添加一些HTML。 Test 1 does not have a <style> tag, while Test 2 does. 测试1没有<style>标记,而测试2没有。

<textarea"><div><style></style><span>Baseline</span></div></textarea>
<textarea id="test1"></textarea>
<textarea id="test2"></textarea>

<script>
    var test1HTML = '<div><span>Test1</span></div>';
    var test2HTML = '<div><style></style><span>Test2</span></div>';

    document.getElementById("test1").innerHTML = test1HTML;
    document.getElementById("test2").innerHTML = test2HTML;
</script>

The above works as expected: all text areas show the HTML code. 上面的代码按预期工作:所有文本区域均显示HTML代码。

But if we use jQuery's .html function to add the content, then Test 2 does not display correctly. 但是,如果我们使用jQuery的.html函数添加内容,则Test 2无法正确显示。 It appears to add the content as actual elements, rather than text. 它似乎将内容添加为实际元素,而不是文本。

$(document).ready(function(){
    $('#test1').html(test1HTML);
    $('#test2').html(test2HTML); // Causes some kind of problem
});

If we replace .html with .text , then both tests again work as expected. 如果将.html替换为.text ,则这两个测试将再次按预期工作。

What's going on with Test 2 using .html ? 使用.html测试2发生了什么? Why does adding a <style> tag change the way .html adds the content? 为什么添加<style>标记会更改.html添加内容的方式? Why do all other methods work correctly? 为什么所有其他方法都能正常工作?

(FWIW - browser is Chrome 39.0.2171.95 m) (FWIW-浏览器为Chrome 39.0.2171.95 m)

jQuery checks the value you're supplying to .html() for <script> , <style> and <link> tags (among several other things). jQuery检查您提供给.html()<script><style><link>标记的值(还有其他一些功能)。 Only if it does not contain them will it continue to set the .innerHTML property of the matched elements. 仅当包含它们时,它才会继续设置匹配元素的.innerHTML属性。

However, if the value does contain any of those tags, then jQuery chooses to execute $(element).empty().append(content); 但是,如果该值确实包含这些标签中的任何一个,则jQuery选择执行$(element).empty().append(content); instead, which appends the text value (node) as a child element. 而是将文本值(节点)附加为子元素。

That should at least explain the difference in behavior. 那至少应该解释行为上的差异。

Here's an excerpt from the source code (v1.11.1): 以下是源代码(v1.11.1)的摘录:

html: function( value ) {
    return access( this, function( value ) {
        var elem = this[ 0 ] || {};
        // ...ommitted
        // See if we can take a shortcut and just use innerHTML
        if ( typeof value === "string" && !rnoInnerhtml.test( value )) //...
            // ...
            elem.innerHTML = value;
            // ...
        }
        // ... else ... 
        this.empty().append( value );
        // .... end if
    }, null, value, arguments.length );
},

The rnoInnerhtml regular expression is: /<(?:script|style|link)/i, rnoInnerhtml正则表达式为: /<(?:script|style|link)/i,

您应该使用.val方法:

$('#test1').val(test2HTML);

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

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