2012-07-09 25 views
0

所以,球员,我只是想格式化使用jQuery的字符串,字符串可闭合的HTML标签<div><h2></h2><span></span></div>或没有它,如<div><h2></h2><span>,或者它可能包含图像,如<img src=""/>,但我需要格式化它只有纯文本的结果,我想这可以用正则表达式来完成,我用它来搜索,但是找不到这样的例子。格式HTML中的jQuery

所以如果你能帮助我,我会非常感谢你的帮助。由于

字符串的为例:

<p> 
    <span style="color: rgb(35, 31, 32); font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 20px; ">Преимущества стоматологической установок Anthos</span> 
</p> 
<ul class="tech_list" style="margin: 0px; padding-right: 0px; padding-left: 0px; list-style-type: none; color: rgb(35, 31, 32); font-family: Arial, Helvetica, sans-serif; "> 
    <li style="margin: 0px; padding: 8px 0px; font-size: 13px; line-height: 18px; overflow: hidden; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); "> 
     <img alt="Anthos " class="photo" src="http 

回答

4
var body; 

try { 
    body = document.implementation.createHTMLDocument().body; 
    //In chrome this avoids requesting images in the html 
} 
catch(e){ 
    body = document.createElement("body"); 
} 
//this doesn't execute css or scripts 
body.innerHTML = '<p> <span style="color: rgb(35, 31, 32); font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 20px; ">Преимущества стоматологической установок Anthos</span></p> <ul class="tech_list" style="margin: 0px; padding-right: 0px; padding-left: 0px; list-style-type: none; color: rgb(35, 31, 32); font-family: Arial, Helvetica, sans-serif; "> <li style="margin: 0px; padding: 8px 0px; font-size: 13px; line-height: 18px; overflow: hidden; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); "> <img alt="Anthos " class="photo" src="http' 

console.log($(body).text()); 
//Преимущества стоматологической установок Anthos 

demo http://jsfiddle.net/FvBzX/

+0

有趣的做法 – Austin 2012-07-09 17:38:41

+0

对不起,但不工作,我更新了我的文章与文字的一个例子。 – Denees 2012-07-09 17:47:26

+0

@DenisHoss工作正常http://jsfiddle.net/FvBzX/,你正在做不同的事情? – Esailija 2012-07-09 17:50:27

2

尝试

$(".example_1").html($(".example_1").text()); 

就拿div的HTML,抢div的文本,并将其放回外容器的HTML 。

被盗从Googled Website

+1

这将在html中执行脚本。在铬中它也会请求图像。 – Esailija 2012-07-09 17:41:25

+0

您的解决方案运行良好,但在某些文本中存在一些小错误,到目前为止您的解决方案是最好的。我用文本示例更新了我的帖子。 – Denees 2012-07-09 17:48:50

0

这个表达式应该从一个字符串中去除所有的HTML标签:

var StrippedString = OriginalString.replace(/(<([^>]+)>)/ig,""); 
+0

对不起,但无法使用,我用文本示例更新了我的帖子。 – Denees 2012-07-09 17:47:31