<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
? ? ? ? "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
? ? <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
? ? <!--<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />-->
? ? <title></title>
? ? <style type="text/css">
? ? ? ? .red{width:100px;height:100px;background:red;}
? ? ? ? .blue{width:100px;height:100px;background:blue;}
? ? </style>
? ? <script type="text/javascript">
? ?/* window.onload=function(){
? ? ? ?alert("Lee");
? ?}
? ? ? ? if(typeof window.onload=="function"){
? ? ? ? ? ? var saved=null;
? ? ? ? ? ? saved=window.onload;
? ? ? ? }
? ? ? ? window.onload=function(){
? ? ? ? if(saved){
? ? ? ? ? ? saved();
? ? ? ? }
? ? ? ? ?alert("Mr.Lee");
}*/
? ?//傳統綁定機制
/* ? window.onload=function(){
? ? ? ? ?var box=document.getElementById("box");
? ? ? ? ? ? box.onclick=function() {
? ? ? ? ? ? ? ? alert("Lee");
? ? ? ? ? ? ? ? toRed().call(this);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? function toRed(){
? ? ? ? ? ?// alert(this)
? ? ? ? ? ? this.className="red";
? ? ? ? ? ? this.onclick=toBlue;
? ? ? ? }
? ? ? ? function toBlue(){
? ? ? ? ? ? this.className="blue";
? ? ? ? ? ? this.onclick=toRed;
? ? ? ? }
*/
? ?/* window["onload"]=function(){
? ? alert("Lee");
? ? }*/
? ? /* function addEvent(obj,type,fn) {
? ? ? ? ?var saved = null;
? ? ? ? ?if (typeof obj['on' + type] == "function") {
? ? ? ? ? ? ?saved = obj['on' + type];//保存上一個事件
? ? ? ? ?}
? ? ? ? ?//運行事件
? ? ? ? ?obj['on' + type] = function () {
? ? ? ? ? ? ?if(saved)saved();//先運行上一個事件
? ? ? ? ? ? ?fn();
? ? ? ? ?}
? ? ?}
? ? ? ?addEvent(window,"load",function(){
? ? ? ?alert("Lee");
? ?})
? ?addEvent(window,"load",function(){
? ? ? ?alert("Leesss");
? ?}) */
*****************風格線*********這里才是本章高潮處*******************************************************************
? ? //當不斷點擊的時候,瀏覽器就會卡死,而且報錯:too muchrecursion,太多的遞歸
? ? //由于積累了太多保存的事件
? ? //解決方式,就是用完事件就馬上清除
? ?//移除事件
? ?function removeEvent(obj,type){
? ? ? ?if(obj["on"+type]) obj["on"+type]=null;
? ?}
? ?//加入事件
? ?function addEvent(obj,type,fn) {
? ? ? ?var saved = null;
? ? ? ?if (typeof obj['on' + type] == "function") {
? ? ? ? ? ?saved = obj['on' + type]; //保存上一個事件
? ? ? ?}
? ? ? ?//運行事件
? ? ? ? obj['on' + type] = function () {
? ? ? ? ? ?if(saved)saved.call(this) ;//先運行上一個事件
? ? ? ? ? ?fn.call(this);/ /這兩個地方要加個call呢 不然一會this就指向window了
? ? ? ?}
? ?}
? ?addEvent(window,'load',function(){
? ? ? ?var box=document.getElementById("box");
? ? ? addEvent(box,'click',toRed); //this 沒有傳遞過去
? ?})
? function toRed(){
? ? ? ?this.className="red";
? ? ? removeEvent(this,'click'); //移除事件函數
? ? ? ?addEvent(this,'click',toBlue) ;//加入事件函數
? ?}
? ?function toBlue(){
? ? ? ?this.className="blue";
? ? ? ?removeEvent(this,'click') ;//移除事件函數
? ? ? ? addEvent(this,'click',toRed); //加入事件函數
? ?}
? ? </script>
</head>
<body>
<div id="box" class="blue">測算點</div>
</body>
? ? ? ? "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
? ? <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
? ? <!--<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />-->
? ? <title></title>
? ? <style type="text/css">
? ? ? ? .red{width:100px;height:100px;background:red;}
? ? ? ? .blue{width:100px;height:100px;background:blue;}
? ? </style>
? ? <script type="text/javascript">
? ?/* window.onload=function(){
? ? ? ?alert("Lee");
? ?}
? ? ? ? if(typeof window.onload=="function"){
? ? ? ? ? ? var saved=null;
? ? ? ? ? ? saved=window.onload;
? ? ? ? }
? ? ? ? window.onload=function(){
? ? ? ? if(saved){
? ? ? ? ? ? saved();
? ? ? ? }
? ? ? ? ?alert("Mr.Lee");
}*/
? ?//傳統綁定機制
/* ? window.onload=function(){
? ? ? ? ?var box=document.getElementById("box");
? ? ? ? ? ? box.onclick=function() {
? ? ? ? ? ? ? ? alert("Lee");
? ? ? ? ? ? ? ? toRed().call(this);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? function toRed(){
? ? ? ? ? ?// alert(this)
? ? ? ? ? ? this.className="red";
? ? ? ? ? ? this.onclick=toBlue;
? ? ? ? }
? ? ? ? function toBlue(){
? ? ? ? ? ? this.className="blue";
? ? ? ? ? ? this.onclick=toRed;
? ? ? ? }
*/
? ?/* window["onload"]=function(){
? ? alert("Lee");
? ? }*/
? ? /* function addEvent(obj,type,fn) {
? ? ? ? ?var saved = null;
? ? ? ? ?if (typeof obj['on' + type] == "function") {
? ? ? ? ? ? ?saved = obj['on' + type];//保存上一個事件
? ? ? ? ?}
? ? ? ? ?//運行事件
? ? ? ? ?obj['on' + type] = function () {
? ? ? ? ? ? ?if(saved)saved();//先運行上一個事件
? ? ? ? ? ? ?fn();
? ? ? ? ?}
? ? ?}
? ? ? ?addEvent(window,"load",function(){
? ? ? ?alert("Lee");
? ?})
? ?addEvent(window,"load",function(){
? ? ? ?alert("Leesss");
? ?}) */
*****************風格線*********這里才是本章高潮處*******************************************************************
? ? //當不斷點擊的時候,瀏覽器就會卡死,而且報錯:too muchrecursion,太多的遞歸
? ? //由于積累了太多保存的事件
? ? //解決方式,就是用完事件就馬上清除
? ?//移除事件
? ?function removeEvent(obj,type){
? ? ? ?if(obj["on"+type]) obj["on"+type]=null;
? ?}
? ?//加入事件
? ?function addEvent(obj,type,fn) {
? ? ? ?var saved = null;
? ? ? ?if (typeof obj['on' + type] == "function") {
? ? ? ? ? ?saved = obj['on' + type]; //保存上一個事件
? ? ? ?}
? ? ? ?//運行事件
? ? ? ? obj['on' + type] = function () {
? ? ? ? ? ?if(saved)saved.call(this) ;//先運行上一個事件
? ? ? ? ? ?fn.call(this);/ /這兩個地方要加個call呢 不然一會this就指向window了
? ? ? ?}
? ?}
? ?addEvent(window,'load',function(){
? ? ? ?var box=document.getElementById("box");
? ? ? addEvent(box,'click',toRed); //this 沒有傳遞過去
? ?})
? function toRed(){
? ? ? ?this.className="red";
? ? ? removeEvent(this,'click'); //移除事件函數
? ? ? ?addEvent(this,'click',toBlue) ;//加入事件函數
? ?}
? ?function toBlue(){
? ? ? ?this.className="blue";
? ? ? ?removeEvent(this,'click') ;//移除事件函數
? ? ? ? addEvent(this,'click',toRed); //加入事件函數
? ?}
? ? </script>
</head>
<body>
<div id="box" class="blue">測算點</div>
</body>
</html>
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
