使用Jquery删除数字预标签

时间:2011-12-17 15:24:01

标签: jquery tags numeric

有没有办法使用Jquery删除覆盖数字的预标签?

例如,我有以下内容:

<pre>1000</pre>

我需要使用Jquery删除PRE标签..

我该怎么做?

提前致谢...

4 个答案:

答案 0 :(得分:4)

(请注意,David Thomas提供了一个如何将.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/

答案 1 :(得分:2)

我认为以下方法可以解开textNode:

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

因为我不知道所有 pre元素是纯数字的,所以我添加了if检查以查看该元素是否包含alpha字符(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

答案 2 :(得分:1)

这么简单:

$("pre").contents().unwrap();

http://jsfiddle.net/c4j77/3/

答案 3 :(得分:0)

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