繁体   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