有一段代码prototype ,直接 在chrome 或是 firefox里不能执行
但是将其内容分别贴在
jsbin.com 网站后,却能得运行正常
是什么原因?求大师指点
代码如下 :<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.1/prototype.js">
</script>
<script type="text/javascript">
var checkBoxWatcher = Class.create();

checkBoxWatcher.prototype = {
initialize: function(chkBox, message){
this.chkbox = $(chkBox);
this.message = message;
this.chkbox.onclick = this.showMessage.bindAsEventListener(this);
},

showMessage: function(evt) {
alert(this.message + '(' + evt.type + ')');
}
};

var checkWatcher = new checkBoxWatcher('mybox', 'i\'m OK!');

</script>
</head><body>
<div id="mybox"> haha </div>
</body>
</html>

解决方案 »

  1.   

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.1/prototype.js">
    </script>
    <script type="text/javascript">
    var Class={create:function(){return new Function();}}
    var checkBoxWatcher = Class.create();checkBoxWatcher.prototype = {
    initialize: function(chkBox, message){
    this.chkbox = $(chkBox);
    this.message = message;
    this.chkbox.onclick = this.showMessage.bindAsEventListener(this);
    },showMessage: function(evt) {
    alert(this.message + '(' + evt.type + ')');
    }
    };var checkWatcher = new checkBoxWatcher('mybox', 'i\'m OK!');</script>
    </head><body>
    <div id="mybox"> haha </div>
    </body>
    </html>
      

  2.   

    文档加载顺序的问题<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.1/prototype.js">
    </script>
    </head><body>
    <div id="mybox"> haha </div><script type="text/javascript">
    var checkBoxWatcher = Class.create();checkBoxWatcher.prototype = {
    initialize: function(chkBox, message){
    this.chkbox = $(chkBox);
    this.message = message;
    this.chkbox.onclick = this.showMessage.bindAsEventListener(this);
    },showMessage: function(evt) {
    alert(this.message + '(' + evt.type + ')');
    }
    };var checkWatcher = new checkBoxWatcher('mybox', 'i\'m OK!');</script>
    </body>
    </html>