css就是取该标签定义的css的属性 attr只能取该标签里自带的属性,而有些标签的里面没有相应的属性 div没有width这个属性,所以不能用attr给他赋值 如下: html代码: <form action="" id="contact-form"> <fieldset> <label><span>姓名:</span><input type="text" /></label><label><span>电子邮件</span><input type="text" /></label> <div class="wrapper"> <span>留言</span> <textarea></textarea> </div> </fieldset> <!-- 操作按钮 --> <div class="wrapper"> <a href="#" class="button" οnclick="javaScript:setColorByjQuery();">JQuery改变颜色</a> </div> </form> 运行js的代码: $(document).ready(function(){ $(".button").click(function(){ //给输入框加背景色,用css可以加相应的背景色 $("input[type='text']").css("background","green"); //用attr加的话是不可以的! $("input[type='text']").attr("background","green"); }) }) 【记住】而#efefef是灰色的
jquery属性css和attr的差别
最新推荐文章于 2022-02-10 19:35:30 发布