简体   繁体   English

如何使用不在div内的jquery删除HTML代码

[英]How to remove HTML code with jquery that is not inside div

I have the below code on my page that I dont have access to and need to remove it. 我的页面上有以下代码,我没有访问权限,需要删除它。 Unfortunately it doesnt sit within a specific div. 不幸的是,它不在特定的div中。

 <style type="text/css" media="screen"> html { margin-top: 32px !important; } * html body { margin-top: 32px !important; } @media screen and ( max-width: 782px ) { html { margin-top: 46px !important; } * html body { margin-top: 46px !important; } } </style> 

How could I remove this with a jquery code? 我怎么能用jQuery代码删除它?

I would really appreciate any help!! 我真的很感谢任何帮助!

The following should do the trick 以下应该做的把戏

$('style[media="screen"]').remove()

if you want it on page load then 如果您希望它在页面加载时

$(document).ready(function() {
  $('style[media="screen"]').remove()
})

Depending on how specific the tag is, you can potentially try targeting it with selectors to find it 根据标签的具体程度,您可以尝试使用选择器将其定位以找到它

var badStyles = $('style[type*="text/css"]');

From here you likely want to determine if it is the correct element as you may have several style elements: 从这里开始,您可能想要确定它是否正确,因为您可能有几个样式元素:

var badStyles = $('style[type*="text/css"]');
badStyles.each(function(i) {
    if($(this).html().indexOf('html { margin-top: 32px !important; }')> 0){
        $(this).empty();  
    }
});

.empty() will remove the contents, however .remove() will remove the entire tag if you so wish. .empty()会删除内容,但是.remove()会删除整个标签(如果您愿意)。

To demonstrate in practice: 在实践中演示:

https://jsfiddle.net/z3g1Le1k/ https://jsfiddle.net/z3g1Le1k/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM