删除
后加倍中断只留下文字

发布于 2024-10-12 09:07:31 字数 372 浏览 4 评论 0原文

在我执行以下操作后:

var noTags = document.createElement("DIV");
noTags.innerHTML = html;
noTags.textContent // <- I put this into any textarea

和此文本:

text test <br><br> this text

变成此文本:

text test 


this text

我如何获得额外的休息。每次我执行上述 JavaScript 操作时,它们都会继续出现。请帮忙:-(

After I perform following:

var noTags = document.createElement("DIV");
noTags.innerHTML = html;
noTags.textContent // <- I put this into any textarea

and this text:

text test <br><br> this text

Turns into this text:

text test 


this text

Some how I get extra break. And they just keep on comming every time I do the above javascript. Please help :-(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等数载,海棠开 2024-10-19 09:07:31

当您将 html 分配给innerHTML 属性时,浏览器将根据页面设置的DOCTYPE 解释该字符串。您将受到浏览器为该 DOCTYPE 实现的解析器以及浏览器对所述标准的实现的支配;换句话说,“你将保证在不同的浏览器上得到不同的结果”。

也就是说,每个浏览器对以下 HTML 的解释略有不同。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Testing textcontent</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" >
</head>
<body>

<form action="">
<textarea id="onlyText" rows="5" cols="40"></textarea>
<script type="text/javascript">
/*<![CDATA[*//*---->*/
var html = "text text <br><br> text";
var noTags = document.createElement("DIV");
noTags.innerHTML = html;

// Retrieve text from html
var textContent = "";
var oe = document.getElementById("onlyText");

if (oe.innerText == undefined) {
    textContent = noTags.textContent;
} else {
    textContent = noTags.innerText;
}

alert("DEBUG: textContent = " + textContent);


oe.innerHTML = textContent;

/*--*//*]]>*/
</script>
</form>

</body>
</html>

欢迎来到互联网。您的代码会有所不同。

When you assign html to the innerHTML property the browser will interpret that string based on the DOCTYPE set for the page. You will be at the mercy of the parser that the browser implemented for that DOCTYPE and the browser's implementation of said standard; read another way, "you will be guaranteed to get different results on different browsers".

That being said each browser will interpret the following HTML slightly differently.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Testing textcontent</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" >
</head>
<body>

<form action="">
<textarea id="onlyText" rows="5" cols="40"></textarea>
<script type="text/javascript">
/*<![CDATA[*//*---->*/
var html = "text text <br><br> text";
var noTags = document.createElement("DIV");
noTags.innerHTML = html;

// Retrieve text from html
var textContent = "";
var oe = document.getElementById("onlyText");

if (oe.innerText == undefined) {
    textContent = noTags.textContent;
} else {
    textContent = noTags.innerText;
}

alert("DEBUG: textContent = " + textContent);


oe.innerHTML = textContent;

/*--*//*]]>*/
</script>
</form>

</body>
</html>

Welcome to the interwebs. Your code will vary.

爱她像谁 2024-10-19 09:07:31

那里有两个休息点,为什么你不希望有两条线呢?

中断通常也作为单个闭合标签完成。
而不是

You have two breaks in there, why wouldn't you expect there to be two lines?

Breaks are usually done as a single closed tag, too. <br/> instead of <br>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文