热门关键字:
jquery > jquery教程 > jquery教程 > 获取CSS属性值

获取CSS属性值

333
作者:管理员
发布时间:2021/1/29 13:52:42
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=3605
您可以通过简单地将属性名称作为参数传递给css()方法来获得元素的CSS属性的计算值。基本语法如下:


$(selector).css(“ propertyName”);
下面的示例将在单击元素时检索并显示元素的CSSbackground-color属性的计算值<div>。


例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery css() Demo</title>
<style>
    div{
        width: 100px;
        height: 100px;        
        display: inline-block;
        margin: 10px;
    }        
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("div").click(function(){
        var color = $(this).css("background-color");
        $("#result").html(color);
    });    
});
</script>
</head>
<body>
    <div style="background-color:orange;"></div>
    <div style="background-color:#ee82ee;"></div>
    <div style="background-color:rgb(139,205,50);"></div>
    <div style="background-color:#f00;"></div>
    <p>The computed background-color property value of the clicked DIV element is: <b id="result"></b></p>
</body>
</html>




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:jQuery
友荐云推荐