我需要从我的 html 页面中找到并删除这个特定标签:
<div style="margin-top: 15px;">Access Denied</div>
我该如何实现这个目标?我看到了以下内容
但我仍然不明白如何从当前整个 html 文档中查找并删除与该字符串匹配的确切标签。
有办法做到这一点吗?
谢谢。
编辑: 我只能修改 :`
的内容<div style="height: 200px;width: 500px; overflow: auto;" id="message">
` 这是页面加载时的整个 html 文档:
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Access Denied</title>
<style type="text/css">.accessdeniedtextfont{FONT-WEIGHT: bolder;FONT-SIZE: 12px;COLOR: #565656;FONT-FAMILY: arial,san-serif;TEXT-DECORATION: none}.accessdeniedcategoryfont{font-weight:bold; FONT-SIZE: 12px;COLOR: #336699;FONT-FAMILY: verdana,arial,san-serif;TEXT-DECORATION: none}</style>
<meta http-equiv="Content-Language" content="en-us">
<script src="/javascript/validation/DefaultDeniedMessagePreview.js" type="text/javascript"/>
</head>
<body style="background: #FFFFFF;font-family: Arial;font-size: 12px;font-weight: bold;color: #565656;">
<form name="previewform">
<table width="100%">
<tbody>
<tr>
<td align="center">
<div style="background: #FFFFFF;margin-top: 9%;box-shadow: 0px 0px 10px #666666;border: 1px solid #CCCCCC;width: 600px;;height:auto">
<table width="100%" cellspacing="0" cellpadding="0" align="center" style="text-align: left;">
<tbody>
<tr>
<td height="15px" colspan="5"> </td>
</tr>
<tr>
<td width="7%"> </td>
<td style="color: #B02B2E;font-size: 20px;font-weight: bold;" colspan="2">
<div style="margin-top: 15px;">Access Denied</div>
</td>
<td height="75" align="right">
<img width="125" height="70" onload="callToSetImageSize(this)" src="/images/customizeimages/denied_topbar.jpeg?v=1445858787891" name="headerimage" id="headerimage">
</td>
<td width="7%"> </td>
</tr>
<tr>
<td align="center" colspan="5">
<div style="width: 98%;height: 2px;margin-bottom: 5px;text-align: center;border-top: 1px solid #CCCCCC;"/>
</td>
</tr>
<tr>
<td> </td>
<td height="30px" colspan="3">
<div style="height: 200px;width: 500px; overflow: auto;" id="message">
<script type="text/javascript"><br/>var divs = document.querySelectorAll("div");<br/><br/>for (var i = 0; i < divs.length; i++) {<br/> if (divs[i].textContent === "Access Denied") {<br/> divs[i].parentNode.removeChild(divs[i])<br/> }<br/>}<br/></script>
<br>
<br>
<font class="accessdeniedtextfont">
<b>
<br>Hello </b>
</font><b>
<font class="accessdeniedcategoryfont">{usr},</font>
<br>
<br>
<br>
<font class="accessdeniedtextfont">This site is unavailable at this time<br>
<br>The page you are trying to access:</font>
<font class="accessdeniedcategoryfont">{url}</font>
<br>
<font class="accessdeniedtextfont">is unavailable. <br>
<br>
<br>
<font class="accessdeniedtextfont">Good bye<br>
</font>
</font>
</b>
</div>
</td>
<td> </td>
</tr>
<tr>
<td align="left" colspan="5">
<div style="width: 98%;height: 60px;margin-bottom: 5px;border-top: 1px solid #CCCCCC;margin-top: 25px;margin-left: 5px;">
<table>
<tbody>
<tr>
<td>
<img width="70" height="60" onload="callToSetImageSize(this)" src="/images/customizeimages/denied_bottombar.jpeg?v=1445858787891" name="footerimage" id="footerimage">
</td>
<td align="center">
<a style="margin-left: 95px;color:#6E400F;font-weight: bold;font-size: 12px;display: none;" id="knowmorelink" href="#">Cyberoam is securing you. Click here to know more.</a>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</form>
<script type="text/javascript">
initDefaultDeniedMessagePreview();
setLabelValue();
</script>
</body>
</html>
最佳答案
尝试使用 document.querySelectorAll("div")
选择所有 div
元素。使用 for
循环检查每个 div
元素是否有 .textContent
匹配“Access Denied”,使用 .removeChild()
如果找到匹配则删除节点
var divs = document.querySelectorAll("div");
for (var i = 0; i < divs.length; i++) {
if (divs[i].textContent === "Access Denied") {
divs[i].parentNode.removeChild(divs[i])
}
}
<div style="margin-top: 15px;">Access Denied</div>
关于javascript - 从页面中删除特定的 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33339040/