簡體   English   中英

使用Javascript從字符串中刪除特定的HTML標簽

[英]Remove Specific HTML Tags from String with Javascript

我有一個帶有一些HTML的字符串。 例...

<tr id="problem_header">
  <td width="50%">Unit 5, Lesson 2: Ordering Decimals</td>
  <td width="50%">Notes</span></td>
</tr>
<tr id="border_row">
  <td class="border_cell" id="border_cell">----</td>
  <td class="border_cell" id="border_cell">----</td>
</tr>

<tr class="problem_display_row">
  <td valign="middle" class="problem_display" id="problem_display">
    Place the following numbers in order from least to greatest. 
    <br><br>
    0.0032, &nbsp;0.0016, &nbsp;0.0097, &nbsp;0.0075, &nbsp;0.0049
  </td>
  <td valign="middle" class="notes_display" id="notes_display">
  </td>
</tr>

<tr class="problem_display_work">
  <td valign="middle" class="problem_display" id="problem_display">
    <span class="emphasis">0.0016, &nbsp;0.0032, &nbsp;0.0049, &nbsp;0.0075, &nbsp;0.0097</span>
  </td>
  <td valign="middle" class="notes_display" id="notes_display">
  Starting with the highest place value, compare each corresponding place value on each number.  Highest number wins!  If there's a tie, move on to the next place value and keep going until you have a winner.
  </td>

使用javascript,我可以按ID取出某些<tr>嗎? 例如,擺脫<tr id="problem_header"> ,但保留其他<tr>

var element = document.getElementById("problem_header");
element.parentNode.removeChild(element);

以字符串格式執行此操作將很困難(使用正則表達式可能很困難,但確實很困難),但是jquery可以處理此問題...

var x = $(YOURSTRING)
//remove values from x with regular jquery calls
return x.html()

還有HTML是字符串格式我想這是更好地執行它首先即顯示所有的tr與一起id="problem_header" ,一旦TR裝入刪除與一個id="problem_header"

您可以將ID刪除為,

var element = document.getElementById("problem_header");
element.parentNode.removeChild(element);

編寫腳本以顯示所有tr元素之后,請確保執行上述腳本。

暫無
暫無

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

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