当前位置: 代码迷 >> JavaScript >> prototype 代码有关问题,求大师帮解
  详细解决方案

prototype 代码有关问题,求大师帮解

热度:85   发布时间:2012-10-10 13:58:11.0
prototype 代码问题,求大师帮解
有一段代码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>





------解决方案--------------------
文档加载顺序的问题
HTML code
<!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> 
  相关解决方案