2016-11-14 73 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解決方案都快。