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


使用 hover() 函式操作 CSS 偽元素。你可以嘗試執行以下程式碼,瞭解如何操作 CSS 偽元素 −

示例

現場演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/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.