gpt4 book ai didi

html - IE 7 错误 - 当文本放置在 innerHTML 中时,嵌入的白色被删除

转载 作者:太空宇宙 更新时间:2023-11-04 16:02:11 25 4
gpt4 key购买 nike

似乎嵌入的空格已从我放入 innerHTML 的文本中删除。有解决办法吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
<script>
function copydiv()
{
var text = document.getElementById("copyfrom").innerHTML;
//In real life "text" is from a AJAX call to a server.
var temp = document.createElement( "TD" );
temp.innerHTML = text
var tr = document.getElementById("copyto");
tr.appendChild(temp);
}

</script>
<head>
<body>
<table>
<tr id="copyto">
<td id="copyfrom">
<a style="white-space: pre;">A Z</a>
</td>
</tr>
</table>
<button type="button" onclick="copydiv();">Test</button>
</body>
</html>

谢谢,格雷

PS:对间距感到抱歉。

最佳答案

//In real life "text" is from a AJAX call to a server.

基本上,您必须将 html 字符串包装在 <pre> 中-元素并在之后将其删除。

// a container
var div = document.createElement('div');
// wrap the text in a <pre>-element
div.innerHTML = '<pre>' + text + '<\/pre>';
var frag = document.createDocumentFragment(),
pre = div.firstChild;
// move all childnodes from the <pre>-element to the DocumentFragment
while(pre.firstChild) {
frag.appendChild(pre.firstChild);
}
// frag now holds the DOM nodes. Add it to the DOM with node.appendChild or
// simliar methods.

完整示例

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head>
<title></title>
<meta http-equiv='X-UA-Compatible' content='IE=EmulateIE7'>
</head>
<body>
<div>
<textarea id='copy-area' cols='20' rows='2'>
&lt;span style="white-space: pre"&gt;| |&lt;/span&gt;
</textarea>
<div id='paste-area'></div>
<button id='add-html'>Add HTML</button>
<button id='alert-innerhtml'>Alert innerHTML</button>
</div>
<script type='text/javascript'>
'use strict';

function addEvent(node, evtType, callback) {
if('addEventListener' in node) {
node.addEventListener(evtType, callback, false);
} else {
node.attachEvent('on' + evtType, callback);
}
}

var copyArea = document.getElementById('copy-area'),
pasteArea = document.getElementById('paste-area');

addEvent(document.getElementById('add-html'), 'click', (function() {
var div = document.createElement('div');
div.innerHTML = '| |';
var useWorkaround = div.innerHTML.length !== 4;
return function(e) {
var text = copyArea.value;
var container;
if(useWorkaround) {
div.innerHTML = '<pre>' + text + '<\/pre>';
container = div.firstChild;
} else {
div.innerHTML = text;
container = div;
}
var frag = document.createDocumentFragment();
while(container.firstChild) {
frag.appendChild(container.firstChild);
}
pasteArea.appendChild(frag);
}
})());

addEvent(document.getElementById('alert-innerhtml'), 'click', function(e) {
alert(pasteArea.innerHTML);
});
</script>
</body>
</html>

使用 iframe 的另一种解决方法

此版本尊重white-space: pre并且在换行时表现得更好。

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head>
<title></title>
<meta http-equiv='X-UA-Compatible' content='IE=EmulateIE7'>
</head>
<body>
<div>
<textarea id='copy-area' cols='20' rows='2'>
&lt;span style="white-space: pre"&gt;| |&lt;/span&gt;
</textarea>
<div id='paste-area'></div>
<button id='add-html'>Add HTML</button>
<button id='alert-innerhtml'>Alert innerHTML</button>
</div>
<script type='text/javascript'>
'use strict';
function addEvent(node, type, callback) {
if('addEventListener' in node) {
node.addEventListener(type, callback, false);
} else {
node.attachEvent('on' + type, callback);
}
}

addEvent(document.getElementById('add-html'), 'click', (function() {
var copyArea = document.getElementById('copy-area'),
pasteArea = document.getElementById('paste-area'),
div = document.createElement('div');

function createFilledFragment(container) {
var frag = document.createDocumentFragment();
while(container.firstChild) {
frag.appendChild(container.firstChild);
}
return frag;
}

div.innerHTML = '| |';
var getFilledFragment = div.innerHTML.length === 4 ?
// Firefox, ...
function(text) {
div.innerHTML = text;
return createFilledFragment(div);
} :
// Workaround IE 7, IE 8, ...
(function() {
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
var body = document.body;
return function(text) {
body.appendChild(iframe);
var iframeDoc = iframe.contentWindow.document;
iframeDoc.write(text);
var frag = createFilledFragment(iframeDoc.body);
body.removeChild(iframe);
return frag;
}
})();

return function(e) {
pasteArea.appendChild(getFilledFragment(copyArea.value));
};
})());

addEvent(document.getElementById('alert-innerhtml'), 'click', (function() {
var pasteArea = document.getElementById('paste-area');
return function(e) {
alert(pasteArea.innerHTML);
}
})());
</script>
</body>
</html>

使用cloneNode并避免这种讨厌的 innerHTML 属性:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
<script>
function copydiv() {
var node = document.getElementById("copyfrom").cloneNode(true);
node.removeAttribute('id');
document.getElementById("copyto").appendChild(node);
}
</script>
<head>
<body>
<table>
<tr id="copyto">
<td id="copyfrom">
<a style="white-space: pre;">A Z</a>
</td>
</tr>
</table>
<button type="button" onclick="copydiv();">Test</button>
</body>
</html>

无论如何它应该更快。

关于html - IE 7 错误 - 当文本放置在 innerHTML 中时,嵌入的白色被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9675675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com