簡體   English   中英

從javascript中的字符串中刪除帶有id的Div,而不刪除其內容

[英]Remove Div with a id from a string in javascript, without removing its content

我需要從javascript中的字符串中刪除a。 div的內容應保持原樣,只需要刪除div標簽。 例如:

我在javascript中跟隨類似的字符串

<div id='abc' style=''> abc fe <div id='someOtherId'>ghgh </div>jjjj </div>

我需要得到的字符串作為

abc fe <div id='someOtherId'>ghgh  </div>jjjj 

謝謝

如果您正在使用jQuery,可以像這樣輕松完成:

var textString = "<div id='abc' style=''> abc fe <div id='someOtherId'>ghgh </div>jjjj </div>";

var htmlObject = $(textString);
var innerHtml = htmlObject.html();

編輯:當不使用jQuery時,這應該也可以

var textString = "<div id='abc' style=''> abc fe <div id='someOtherId'>ghgh </div>jjjj </div>";
var tempObject = document.createElement("div");
tempObject.innerHTML = textString;
var innerHtml = tempObject.firstChild.innerHTML;

嘗試這個

<div id='abc' style=''> abc fe <div id='someOtherId'>ghgh </div>jjjj </div>
<p></p>
<script>
    var content=$('#abc').hmtl();
    $('p').append(content);
</script>

jsfiddle測試希望這對你有所幫助

試試這個代碼:

<div id='abc' style=''> <span id="txttoshow">abc fe <div id='someOtherId'>ghgh </div>jjjj </div></span>

jquery代碼:

$('span').unwrap();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM