从JQuery中的字符串提取HTML内容
我有一个textarea,在这个textarea中,动态HTML内容是绑定的从JQuery中的字符串提取HTML内容,jquery,Jquery,我有一个textarea,在这个textarea中,动态HTML内容是绑定的 <div id="HDWebAllTemplateHTMLListMessage"> <div id="header">This is a header</div> <div class="main"> <div id="content">This is my main content.</div> <
<div id="HDWebAllTemplateHTMLListMessage">
<div id="header">This is a header</div>
<div class="main">
<div id="content">This is my main content.</div>
<div id="sidebar">This is a sidebar</div>
</div>
<div id="footer">This is my footer</div>
</div>
text()
提供的是文本内容,而不是html,因此用$()
包装它是行不通的。使用html()
$('#hdweballtemplatehtmlistmessage.main').html()
您可以通过将js脚本更改为以下命令来获得结果
<script>
var textAreaContent=$('#HDWebAllTemplateHTMLListMessage > .main').html();
console.log(textAreaContent);
</script>
var textAreaContent=$('#hdweballtemplatehtmlistmessage>.main').html();
console.log(textAreaContent);
下面是一个运行示例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="HDWebAllTemplateHTMLListMessage">
<div id="header">This is a header</div>
<div class="main">
<div id="content">This is my main content.</div>
<div id="sidebar">This is a sidebar</div>
</div>
<div id="footer">This is my footer</div>
</div>
<script>
var textAreaContent=$('#HDWebAllTemplateHTMLListMessage > .main').html();
console.log(textAreaContent);
</script>
</body>
</html>
这是一个标题
这是我的主要内容。
这是一个侧边栏
这是我的页脚
var textAreaContent=$('#hdweballtemplatehtmlistmessage>.main').html();
console.log(textAreaContent);
您真的需要使用textarea吗
你可以试试下面的方法。但如果你不需要它。azizsagi示例运行良好
JAVASCRIPT
var newdiv1 = $("<div class='dummy'></div>");
$("textarea").after(newdiv1); /* create dummy div */
var tgt = newdiv1;
var main = tgt.html($("textarea").text()).find(".main").text();
tgt.text(main);
/* newdiv1.remove(); */ // If you want to remove the element after you get what you need
var newdiv1=$(“”);
$(“textarea”)。在(newdiv1)之后;/*创建虚拟div*/
var tgt=newdiv1;
var main=tgt.html($(“textarea”).text()).find(“.main”).text();
tgt.text(main);
/*newdiv1.remove();*//如果要在获得所需内容后删除元素
jsIDLE:
#hdweballtemplatehtmlistmessage
不在您发布的代码中,也不在.23TC
,什么是htmlContent
?@我已经更新了我的question@Anjyr但是什么是#hdweballtemplatehtmlistmessage
?jQuery的find()
函数返回一个jQuery对象,而且它没有名为innerHTML
的属性,请改用html()
。另外,jQuery的text()
函数不返回HTML标记-同样,使用HTML()
。看见
var newdiv1 = $("<div class='dummy'></div>");
$("textarea").after(newdiv1); /* create dummy div */
var tgt = newdiv1;
var main = tgt.html($("textarea").text()).find(".main").text();
tgt.text(main);
/* newdiv1.remove(); */ // If you want to remove the element after you get what you need