2016-11-14 74 views
1

目前在使用jquery删除强标签方面存在一些小问题。通过jquery从jquery删除严格强大/粗体的文本标签

我想保留强大的标签之间的文字,所以如: 前:

<strong>This is a text</strong> 

后:

this is a text 

和代码:

$("div.row.ordertype").each(function(){ 
      if(($(this).text()).length>0){ 
      console.log($(this).html()); 
      var test = $("<strong>","<strong />").contents().unwrap(); 
      console.log(test); 
      }  
    }) 

任何想法

回答

2

应该是

$("strong").contents().unwrap();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<strong>This is a text</strong>

的选择$("<strong>","<strong />")创造了新的元素,它不会选择元素

+0

这工作。非常感谢。不知道为什么我被拒绝了,因为这个问题包含了所有必要的信息:/ –

+0

@MubeenHussain - 不客气! – adeneo

3

使用下面的jQuery

$('strong').contents().unwrap(); 

为了更快的性能,始终低于使用:

var strong = document.getElementsByTagName('strong'); 
while(strong.length) { 
    var parent = strong[0].parentNode; 
    while(strong[0].firstChild) { 
     parent.insertBefore(strong[0].firstChild, strong[0]); 
    } 
    parent.removeChild(strong[0]); 
} 

这比任何jQuery解决方案都快。