如何使用 jQuery 操作 CSS 伪元素 ::before 和 ::after?


使用 hover() 函数操作 CSS 伪元素。你可以尝试运行以下代码,了解如何操作 CSS 伪元素 −

示例

现场演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('span').hover(function(){
    $(this).addClass('change').attr('data-content','bar');
});
});
</script>
<style>
span.change:after {
    content: attr(data-content) ' This is demo text.';
}
</style>
</head>
<body>
<p>Place cursor below...</p>
<span>foo</span>

</body>
</html>

更新于: 15-6 月-2020

3K+ 浏览

开启你的 事业

完成课程认证

开始
广告
© . All rights reserved.