1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <p id="title">box</p> <input id="button1" type = "button" value = "changeStyle" /> <script>
//写一个函数,用以传入一个DOM对象,,传入一个mJson,然后赋值。 function css(obj , mJson) { for(var i in mJson){ obj.style[i] = mJson[i]; } } //-------测试------ var pBox = document.getElementById('title'); var btn = document.getElementById('button1'); var mJson = { 'width':'300px', 'height':'100px', 'backgroundColor':'red' } btn.onclick = function() { css(pBox,mJson); } </script>
|