[英]Remove all html tags and javascript tags using regex in javascript
[英]Remove background style in all html tags using javascript
如何刪除所有HTML標記中的所有background="..."
? 我知道嘗試使用正則表達式解析HTML不是一個好主意,但我對此一無所知。 (嘗試過正則表達式"background([\\s\\S]*?)(;|\\")"
-不起作用。)謝謝。
通過簡單的腳本,您可以使用此代碼,
<script>
var allElements = document.childNodes;
for (var i = 0; i < allElements.length; i++)
{
var currElement = allElements[i];
if (currElement.hasAttribute("background"))
{
currElement.removeAttribute("background");
}
}
</script>
and to solve your mentioned issue,
use this,
use this to solve this issue,
`<script>
function removeBackground(cElement) {
var allElements = cElement.childNodes;
if (allElements.length > 0) {
for (var i = 0; i < allElements.length; i++) {
var currElement = allElements[i];
if (currElement.childNodes.length > 0) {
removeBackground(currElement);
}
if (currElement.hasAttribute("background")) {
currElement.removeAttribute("background");
}
}
}
}
removeBackground(document);
</script>`
$(document).ready(function(){ $('*').removeAttr('background'); });
<!DOCTYPE html> <html> <body background="https://www.google.co.in/images/srpr/logo11w.png"> <h1>Hello world!</h1> <p>The background attribute is not supported HTML5. Use CSS instead.</p> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </body> </html>
您可以使用jquery刪除所有背景屬性,例如,
$('*').removeAttr("background");
使用jQuery,您可以為此使用一個簡單的選擇器:
$("*").css('background', 'transparent');
背景屬性而不是樣式:
$("*").removeAttr('background');
如果沒有jQuery,則還有更多工作要做:
var elements = document.getElementsByTagName("*");
for (var i = 0, i < elements.length; i++) {
elements[i].style.background = "transparent";
}
原始背景屬性:
var elements = document.getElementsByTagName("*");
for (var i = 0, i < elements.length; i++) {
if (elements[i].hasAttribute("background")) {
elements[i].removeAttribute("background");
}
}
您可以添加一個自定義工作表來覆蓋背景屬性,如下所示
var style = document.createElement('style');
document.head.appendChild(style);
// Depend on browser implementation
if (style.sheet.insertRule) {
style.sheet.insertRule("* { background:none !important }", 0);
}
else if (style.sheet.addRule) {
style.sheet.addRule("*","background:none !important", 0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.