JavaScript,这是实例方法吗?
我就看看,我不说话。
这是 Box.js 文件:
/// <reference path="jquery-1.8.2.min.js" /> function Box(input) { var me = this; this.things = input; this.say = "You got a " + me.things;
this.onclick = function (sender) { alert(me.say); };
this.putOn = function (stage) { $(stage).html("Here is a box with " + me.things+"s."); stage.onclick = function () { return me.onclick(me); }; } }
使用 Box
<html> <head> <title>JavaScript Box</title> <style type="text/css"> div { border: 1px solid gray; width: 220px; height: 50px; text-align:center; line-height:50px; cursor:pointer;} </style> <script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script> <script src="Scripts/Box.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var a = new Box("pear"); a.putOn($("#div1").get(0)); var b = new Box("book"); b.putOn($("#div2").get(0)); b.onclick = function (sender) { alert(sender.say + ", but it is not yours." ); }; }); </script> </head> <body> <div id="div1"></div> <br /> <div id="div2"></div> </body> </html>
效果:
--