常见样式和属性设置函数代码如下:
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> .box1{ width: 200px; height: 200px; background-color: lightgray; display: table-cell; vertical-align: middle; border-radius: 4px; box-shadow: 2px 2px 2px #888; } .box2{ width: 120px; height: 120px; background-color: gray; margin: auto; border-radius: 7px; } </style> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> </head> <body> <div class="box1"> <div class="box2"><img class="pic" src="https://www.llheng.com/img/img/boy.jpeg" width="100" alt="test" title="animal"></div> </div> </body> </html> <script type="text/javascript"> var s = $('img').attr('src') var s = $('.pic').attr({ "width":"120", "style":"box-shadow:2px 2px 2px #888" }) var s = $('.pic').attr('title',function(){ return 'lovely' }) var s = $('.box1').css('background-color') var s = $('.pic').css({ "box-shadow":"3px 3px 3px #888", "border-radius":"7px" }) var s = $('.box2').width() var s = $('.box1').height() var s = $('.box2').offset().top var s = $('.pic').position().top //console.log(s) </script>
运行实例 »
点击 "运行实例" 按钮查看在线实例
手写css方法如下:
总结:
css()和attr()类似类似,当参数只有一个时,函数的作用是获取参数属性的值,当参数为2个值时,作用是设置相应的参数,如果设置的属性值有多对,用{}包含,用逗号隔开。