2014-12-03 131 views
0

如何以编程方式使用Jquery删除此'content'?删除元素CSS前的内容

body:before { 
    content: ''; 
    display: inline-block; 
    height: 100%; 
    vertical-align: middle; 
    margin-right: -0.25em; 
} 
+4

你不能用JavaScript操作CSS伪元素,因为它们不是技术上DOM的一部分。 – George 2014-12-03 11:02:51

+0

我可以删除这个使用CSS? – wawanopoulos 2014-12-03 11:03:29

+0

'body:before {display:none; }' – 2014-12-03 11:04:44

回答

3

这是不可能直接访问伪元素jQuery的,因为他们不是DOM的一部分。

但是作为一种解决方法,你可以立足伪元素上的一类对人体,如:

body:before { 
    content: ''; 
    display: inline-block; 
    height: 100%; 
    vertical-align: middle; 
    margin-right: -0.25em; 
} 
body.foo:before { 
    display: none; 
} 

然后隐藏元素的jQuery:

$('body').addClass('foo'); 
+0

这里是一个小提琴伪元素被删除的身体上的点击事件:http://jsfiddle.net/webtiki/6h5awgky/ – 2014-12-03 11:07:56

+0

@ web-tiki谢谢。我向psuedo元素添加了一些内容,使其更加明显发生了什么:http://jsfiddle.net/6h5awgky/1/ – 2014-12-03 11:10:10

+0

不错。我很喜欢这个。 – lharby 2014-12-03 11:26:36

0

你可以将其覆盖:

document.styleSheets[0].addRule('body:before', 'display: none !important;'); 
0

使用JavaScript可以修改body:beforedisplay道具并将其设置为none

检查元素以查看修改的规则。

var ss = document.styleSheets; 
 

 
for (i = 0; i < ss.length; i++) { 
 
    var rules = ss[i]; 
 
    for (j = 0; j < rules.cssRules.length; j++) { 
 
    var r = rules.cssRules[j]; 
 
    if (r.selectorText == "body:before") { 
 
     console.log('Before: ' + r.cssText); 
 
     r.style.display = 'none'; 
 
     console.log('After: ' + r.cssText); 
 
    } 
 
    } 
 
}
body:before { 
 
    content: ''; 
 
    display: inline-block; 
 
    height: 100%; 
 
    vertical-align: middle; 
 
    margin-right: -0.25em; 
 
}