首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

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

2012-10-10 
prototype 代码问题,求大师帮解有一段代码prototype ,直接 在chrome 或是 firefox里不能执行但是将其内容

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> 

热点排行