jquery

jquery怎么获取背景颜色

养鱼的猫咪
1002
2021-05-25 16:53:29
栏目: 编程语言

使用jquery获取背景颜色的方法:1.新建html项目,引入jquery;2.创建div标签,设置id属性;3.设置宽高和背景颜色;4.添加button按钮,绑定onclick点击事件;5.通过id获取标签对象,使用css()方法获取背景颜色;


jquery怎么获取背景颜色


具体步骤如下:

1.首先,新建一个html项目,并在项目中引入jquery;


<script type="text/javascript" src="/static/jquery-2.1.4.min.js"></script>


2.引入jquery后,在项目中创建一个div标签,并设置id属性,用于测试;


<div id="text"></div>


3.div标签创建好后,为标签设置宽高和背景颜色;


<div id="text" style="width:100px;height:20px;background-color:red"></div>


4.标签样式设置好后,添加一个button按钮,并绑定onclick点击事件,用于点击获取背景颜色;


<button onClick="set()"><button>


5.最后,按钮添加好后,在点击事件中通过id获取标签对象,在使用css()方法即可获取到背景颜色;


function set(){

var res = $("#text").css("background-color");

alert(res);

}



0
看了该问题的人还看了