2011-12-17 80 views
1

有沒有什麼辦法去除使用Jquery覆蓋數字的前置標籤?用Jquery去除數字前置標籤

例如,我有以下內容:

<pre>1000</pre> 

我需要刪除使用jQuery的PRE標籤..

我怎樣才能做到這一點?

在此先感謝...

回答

4

(注意,大衛·托馬斯提供瞭如何使用.unwrap()firstchild一個很好的例子。)

.unwrap()

$("pre").wrapInner('<div>').find('div').unwrap(); 

http://jsfiddle.net/c4j77/

你也可以這樣做:

$("pre").parent().each(function(){ 
    $(this).text($(this).text()); 
}); 

http://jsfiddle.net/c4j77/1/

0
$("#element").html($("#element").html().replace(/<\/?pre>/i, '')); 
2

我認爲下面會的工作,解開了textNode:

$('pre').each(function(){ 
    $(this.firstChild).unwrap(); 
}); 

因爲我不知道,如果所有pre元素將爲純數字,我添加了if檢查以查看元素是否包含字母字符(az,大寫和小寫):

$('pre').each(function(){ 
    var text = $(this).text(); 
    if (text.match(/[a-z]/gi)){ 
     // you've got a string with letters 
    } 
    else { 
     $(this.firstChild).unwrap(); 
    } 
}); 

JS Fiddle demo

+0

忽略之前的評論,我有不正確的標記;這至少在FF8中起作用。演示:http://jsfiddle.net/8P6Fq/ – 2011-12-17 15:38:20

+0

+1提供了很好的示例 – Fero 2011-12-19 04:47:30