如何使用 jQuery 访问伪元素的样式属性?
对于上下文,这是之前的问题的后续问题。我不想深入研究 cssRules
,而是希望将逻辑建立在搜索这些规则的效果的 jQuery 选择器上。
给定的默认属性
.commentarea .author:before {
background-image: url(http://...);
background-position: -9999px -9999px;
/* ... */
}
是有选择地修改的,例如
.author[href$="gbacon"]:before /* ... */ {
content: "";
background-position: 0 -140px
}
如何选择各自背景位置具有默认值的伪元素?复制选择器就像
GM_log("size = " + $(".commentarea .author:before").size());
没有匹配任何内容。尝试 .siblings()
$(".commentarea .author")
.map(function(i) {
GM_log($(this)
.siblings()
.map(function (i) { return $(this).css("background-image") })
.get()
.join(", "))
});
仅产生 none值。
如需了解完整详情,请参阅实时页面。这可能吗?
For context, this is a followup to an earlier question. Rather than digging through cssRules
, I'd like to base the logic on jQuery selectors that search for the effects of those rules.
Given default properties of
.commentarea .author:before {
background-image: url(http://...);
background-position: -9999px -9999px;
/* ... */
}
that are selectively modified as in
.author[href$="gbacon"]:before /* ... */ {
content: "";
background-position: 0 -140px
}
how can I select pseudo-elements whose respective background positions have default values? Copying the selector as in
GM_log("size = " + $(".commentarea .author:before").size());
matches nothing. Trying .siblings()
with
$(".commentarea .author")
.map(function(i) {
GM_log($(this)
.siblings()
.map(function (i) { return $(this).css("background-image") })
.get()
.join(", "))
});
produces only none
values.
For full details, see the live page. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能像这样使用
:before
和:after
伪元素。它们的目的是在您指定的选择器之前和之后(分别)插入内容。示例用法:
HTML:
CSS:
结果:
http://jsfiddle .net/mzcp6/
发生的情况是,文本
|Inserted using :before|
被插入到内部跨度之前(嗯,实际上是前置),因为它是类b
以及类a
的元素的后代。基本上,:before
和:after
不选择;他们修改。示例:
这不会按预期工作:
HTML:
CSS:
没有任何反应:
http:// jsfiddle.net/bQ2ty/
编辑:
:before
不是有效的 jQuery 选择器:http://api.jquery.com/category/selectors/我认为您需要使用
:before
或尝试之外的其他内容使用 jQuery 插件提取原始规则: http://flesler.blogspot.com/2007 /11/jqueryrule.htmlYou can't use the
:before
and:after
pseudo-elements like this. The purpose of them is to insert content before and after (respectively) the selector you have specified.Example usage:
HTML:
CSS:
Result:
http://jsfiddle.net/mzcp6/
What happened was that the text
|Inserted using :before|
was inserted before (well, really, prepended into) the inner span because it was classb
and a descendant of an element of classa
. Basically,:before
and:after
don't select; they modify.Example:
This doesn't work as expected:
HTML:
CSS:
Nothing happens:
http://jsfiddle.net/bQ2ty/
EDIT:
:before
is not a valid jQuery selector: http://api.jquery.com/category/selectors/I think you will need to use something other than
:before
or attempt to extract the original rule using the jQuery plugin: http://flesler.blogspot.com/2007/11/jqueryrule.html