日期:2014-05-16 浏览次数:20411 次
<!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> <title></title> <script src="http://code.jquery.com/jquery-latest.js"></script> <style> </style> </head> <body> <div id="a">333</div> <script> var opts = { color: 'red', fontSize: 40 }; $('#a').css(opts); </script> </body> </html>
------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> (function ($) { $.fn.bgColor = function (value) { this.css("background-color", value); }; })(jQuery); function test() { $('#divTest').bgColor("red"); } </script> </head> <body> <div id="divTest"> ABC </div> <input type="button" onclick="test()" value="用插件实现直接赋值" /> </body> </html>
------解决方案--------------------
var attr = "width"; $('#id').css(attr, 100); // 或者 var attr = "width", property = {}; property[attr] = 100; $('#id').css(property);