<html> <head> <style type="text/css"> div#mydiv { width: 100px; height: 100px; background-color: red; } </style> <script language="JavaScript"> function addWidth() { var mydiv = document.getElementById("mydiv"); alert(mydiv.style.width+","+parseInt(mydiv.style.width)); var curr_width = parseInt(mydiv.style.width); // removes the "px" at the end mydiv.style.width = (curr_width + 10) +"px"; } </script> </head> <body> <input type="button" value="Make Bigger" οnclick="addWidth()" /> <div id="mydiv" style="width:100px"></div> </body> </html>
主要是这句: var curr_width = parseInt(mydiv.style.width); // removes the "px" at the end
是不是很有意思?