Javascript 有没有办法将 HTML 转换为普通文本,而无需使用 Jquery 将其实际写入选择器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8299742/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:28:00  来源:igfitidea点击:

Is there a way to convert HTML into normal text without actually write it to a selector with Jquery?

javascriptjqueryhtmldom

提问by eastboundr

I understand so far that in Jquery, with html() function, we can convert HTML into text, for example,

到目前为止,我了解在 Jquery 中,使用 html() 函数,我们可以将 HTML 转换为文本,例如,

$("#myDiv").html(result);

converts "result" (which is the html code) into normal text and display it in myDiv.

将“结果”(即 html 代码)转换为普通文本并在 myDiv 中显示。

Now, my question is, is there a way I can simply convert the html and put it into a variable?

现在,我的问题是,有没有一种方法可以简单地转换 html 并将其放入变量中?

for example:

例如:

var temp;
temp = html(result);

something like this, of course this does not work, but how can I put the converted into a variable without write it to the screen? Since I'm checking the converted in a loop, thought it's quite and waste of resource if keep writing it to the screen for every single loop.

像这样的东西,当然这不起作用,但是如何将转换后的变量放入变量而不将其写入屏幕?由于我在循环中检查转换,因此认为如果每次循环都将其写入屏幕,则相当浪费资源。

Edit:

编辑:

Sorry for the confusion, for example, if result is " <p>abc</p> "then $(#mydiv).html(result)makes mydiv display "abc", which "converts" html into normal text by removing the <p>tags. So how can I put "abc"into a variable without doing something like var temp=$(#mydiv).text()?

很抱歉造成混淆,例如,如果结果是" <p>abc</p> "然后$(#mydiv).html(result)使 mydiv 显示"abc",它通过删除<p>标签将 html“转换”为普通文本。那么如何"abc"在不执行类似操作的情况下放入变量var temp=$(#mydiv).text()

采纳答案by Guffa

No, the htmlmethod doesn't turn HTML code into text, it turns HTML code into DOM elements. The browser will parse the HTML code and create elements from it.

不,该html方法不会将 HTML 代码转换为文本,而是将 HTML 代码转换为 DOM 元素。浏览器将解析 HTML 代码并从中创建元素。

You don't have to put the HTML code into the page to have it parsed into elements, you can do that in an independent element:

您不必将 HTML 代码放入页面以将其解析为元素,您可以在独立元素中执行此操作:

var d = $('<div>').html(result);

Now you have a jQuery object that contains a divelement that has the elements from the parsed HTML code as children. Or:

现在您有一个 jQuery 对象,其中包含一个div元素,该元素将已解析的 HTML 代码中的元素作为子元素。或者:

var d = $(result);

Now you have a jQuery object that contains the elements from the parsed HTML code.

现在您有一个 jQuery 对象,其中包含来自已解析 HTML 代码的元素。

回答by Crozin

You could simply strip all HTML tags:

您可以简单地去除所有 HTML 标签:

var text = html.replace(/(<([^>]+)>)/g, "");

回答by Diodeus - James MacFarlane

Why not use .text()

为什么不使用 .text()

$("#myDiv").html($(result).text());

回答by Finesse

Here is no-jQuery solution:

这是无 jQuery 解决方案:

function html2text( html ) {
    var d = document.createElement( 'div' );
    d.innerHTML = html;
    return d.textContent;
}

Works great in IE ≥9.

在 IE ≥9 中效果很好。

回答by Larry Cinnabar

you can try:

你可以试试:

var tmp = $("<div>").attr("style","display:none");
var html_text = tmp.html(result).text();
tmp.remove();

But the way with modifying string with regular expression is simpler, because it doesn't use DOM traversal.

但是用正则表达式修改字符串的方式更简单,因为它没有使用DOM遍历。

You may replace html to text string with regexp like in answer of user Crozin.

您可以使用正则表达式将 html 替换为文本字符串,就像 user 的回答一样Crozin

P.S.Also you may like the way when <br>is replacing with newline-symbols:

PS你也可能喜欢<br>用换行符替换的方式:

var text = html.replace(/<\s*br[^>]?>/,'\n')
            .replace(/(<([^>]+)>)/g, "");

回答by Toni Toni Chopper

var temp = $(your_selector).html();

var temp = $(your_selector).html();

the variable temp is a string containing the HTML

变量 temp 是一个包含 HTML 的字符串

回答by Tim Joyce

$("#myDiv").html(result);is not formatting text into html code. You can use .html()to do a couple of things.

$("#myDiv").html(result);不是将文本格式化为 html 代码。你可以.html()用来做几件事。

if you say $("#myDiv").html();where you are not passing in parameters to the `html()' function then you are "GETTING" the html that is currently in that div element. so you could say,

如果你说$("#myDiv").html();你没有将参数传递给 `html()' 函数,那么你就是在“获取”当前在该 div 元素中的 html。所以你可以说,

var whatsInThisDiv = $("#myDiv").html();
console.log(whatsInThisDiv); //will print whatever is nested inside of <div id="myDiv"></div>

if you pass in a parameter with your .html()call you will be setting the html to what is stored inside the variable or string you pass. For instance

如果您在.html()调用时传入参数,您会将 html 设置为存储在您传递的变量或字符串中的内容。例如

var htmlToReplaceCurrent = '<div id="childOfmyDiv">Hi! Im a child.</div>';
$("#myDiv").html(htmlToReplaceCurrent);

That will leave your dom looking like this...

那会让你的dom看起来像这样......

<div id="myDiv">
    <div id="childOfmyDiv">Hi! Im a child.</div>
</div>