如何使用 jQuery 访问伪元素的样式属性?

发布于 2024-09-19 21:12:38 字数 1207 浏览 7 评论 0原文

对于上下文,这是之前的问题的后续问题。我不想深入研究 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2024-09-26 21:12:38

您不能像这样使用 :before:after 伪元素。它们的目的是在您指定的选择器之前和之后(分别)插入内容。

示例用法:

HTML:

<span class='a'>
    Outer
    <span class='b'>
        Inner
    </span>
</span>

CSS:

.a .b:before {
    content: "|Inserted using :before|";
}

.a {
    color: blue;
}

.b {
    color: red;
}

结果:

http://jsfiddle .net/mzcp6/

发生的情况是,文本 |Inserted using :before| 被插入到内部跨度之前(嗯,实际上是前置),因为它是类 b 以及类a 的元素的后代。基本上, :before:after 不选择;他们修改。

示例:

这不会按预期工作:

HTML:

<span class='a'>
    <p>More text</p>
    <span class='b'>
        <p>More text</p>
        Inner
    </span>
</span>

CSS:

.a .b:before {
    text-size: 100px;
}

没有任何反应:

http:// jsfiddle.net/bQ2ty/

编辑:

:before 不是有效的 jQuery 选择器:http://api.jquery.com/category/selectors/

我认为您需要使用 :before 或尝试之外的其他内容使用 jQuery 插件提取原始规则: http://flesler.blogspot.com/2007 /11/jqueryrule.html

You 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:

<span class='a'>
    Outer
    <span class='b'>
        Inner
    </span>
</span>

CSS:

.a .b:before {
    content: "|Inserted using :before|";
}

.a {
    color: blue;
}

.b {
    color: red;
}

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 class b and a descendant of an element of class a. Basically, :before and :after don't select; they modify.

Example:

This doesn't work as expected:

HTML:

<span class='a'>
    <p>More text</p>
    <span class='b'>
        <p>More text</p>
        Inner
    </span>
</span>

CSS:

.a .b:before {
    text-size: 100px;
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文