jquery - 使用像 :after with $(this) 这样的伪元素

标签 jquery css pseudo-element

<分区>

例如,我有这个 jQuery 函数:

function example(){

  var trigger = $('.image');

  trigger.each(function(e){
    // here I want to change the background color of the :after pseudo-element
    $(this:after).css('background-color', 'red');
    // ^ the above obviously doesn't work
  });

}

如何添加、更改或删除 $(this) 的伪元素的 CSS 属性?

最佳答案

您不能处理“:after”伪元素,因为它在 DOM 中不存在(与 :before 相同)。但是您可以创建它:

$(element).after();

然后用 JS 处理。

希望对你有用!

关于jquery - 使用像 :after with $(this) 这样的伪元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40177108/

上一篇:javascript - 如何在 <div> 标签的背景中提供不同的视频?

下一篇:html - 在 div 中垂直对齐

相关文章:

html - CSS 首字母高亮显示

html - 伪元素将高度插入带有文本的 Div

javascript - 图片背景点击

javascript - 当类型值相等时如何禁用字段

javascript - 动态文档对象模型 (DOM) 的后退按钮(历史记录)

jquery - 在 HTML 元素上应用多个 CSS 类 (jQuery addClass)

jquery - CSS 导航栏屏幕分辨率

css - Bootstrap container-fluid 不是屏幕的整个宽度

css - 带有 :before and :after 的背景图像

javascript - 如何将多个参数传递给函数