JavaScript日誌操作物件實例
現在我們來綜合使用一下所學知識,來實現一個JavaScript日誌操作對象的實例,我們需要的是這幾個文件:
myLog.js:主要作用是構建myLogger構造函數、添加行、添加節點、 css控制。
LD.js:主要功能是為腳本和字串制定規則、建構命名空間和依據Id、className取出物件。
test.js:主要功能是對表單新增事件,並測試mylog中部分函數的可用性。
log.html:用於顯示日誌物件。
以下是各個檔案程式碼:
test.js
[javascript] view plaincopyprint?//在window物件裡面加上一個load事件
LD.addEvent(window,load',function(){
LD.addEvent(window,'loadload',function(){ .writeRaw('This is raw');
LD.log.writeRaw('This is bold!');
LD.log.write('write source:This is bold!');
for(i in document){ }
});
//在window物件裡面新增一個load事件
LD.addEvent(window,'load',function(){
LD.log.writeRaw('This is raw');
LD.log.writeRaw('This is bold!');
LD.log.header('With a header');
LD.log.write('write source:This is bold!');
for(i in document){
myLog.js[javascript] view plaincopyprint?// JavaScript Document
//myLogger的建構子
function myLogger(id){
var logWindow =null;
//建立日誌窗體
var createWindow=function(){
var top=(browserWindowSize.height-200)/2||0;
var left=( browserWindowSize.width-200)/2||0;
//使用UL
//在document下方新增dom物件UL
//新增ID識別進行識別 logWindow.setAttribute("id",id);
//對視窗進行css樣式控制
logWindow.style.top=top+'px';
logWindow.style. left=left+'px';
logWindow.style.width='200px' dow
logWindow.style.overflow='scroll';
0';
logWindow.style.margin='0';
logWindow.style.backgroundColor='white';
logWindow.style.listStyle='none';
';
logWindow.style.font='10px/10px Verdana, Tahoma, Sans';
//將視窗新增至頁面 ).
}
//新增一行
this.writeRaw=function(message){
//如果初始視窗是使用日誌 createWindow();
}
//建立li的dom節點
var li=document.createElement('LI');
//對窗體進行css樣式控制 ='2px';
li.style.border='0';
li. style.borderBottom='1px dotted black';
li.style.margin='0';
// 驗證message資訊
if(typeof message == 'undef
li.appendChild(
);
li.innerHTML=message ;
}else{
}
return true;
};
}
//物件字面量的方式宣告特權方法
//在日誌窗體中新增一行,為輸入的內容進行簡單處理
myLogger.prototype={
write:function(message){N
write: ){
if(typeof message !='string'){
if(message.toString){ age.toString()) ;
}else{
}
}
//將大於號小於號進行正規轉換為HTML replace(/>/g,">");
return this.writeRaw(message);
},
background-color:black ;font-weight:bold;padding:0px 5px;">'+message+'';
return this.writeRaw(message); 製作= new myLogger();
// JavaScript Document
//myLogger的建構子
function myLogger(id){
id=id||"ICLogWindow";
//日誌窗體的參考
var logWindow=null;
//建立日誌窗體
var createWindow=function(){
dowSize.height-200) /2||0;
var left=(browserWindowSize.width-200)/2||0;
logWindow=document.createElement("UL");物件UL
//新增ID進行標誌
dow.style.top =top+'px';
logWindow.style.width='200px';
';
logWindow.style.padding='0';
logWindow.style.margin='0';
logWindow.style logWindow.style.listStyle='none';
logWindow.style.font='10px/10px Verdana, Tahoma, Sans';
//將窗體加到頁面上
); }
//在日誌視窗中新增一行
this.writeRaw=function(message){
//如果初始視窗是不存在的,產生日誌;
//創建li的dom節點
var li=document.createElement('LI');
li.style.borderBottom='1px dotted black';
li.style.margin='0';
li.style.color='#000';訊息
if(typeof message == 'undefined'){
//在li內加入文字節點。
li.appendChild(
li.appendChild(
document.createTextNode('Message is undefined')
//這是另一種方式的表達
li.innerHTML=message ;
}else{
li.appendChild(
document.createTextNode logWindow.appendChild(li);
return true;
};
}
//物件字面量的方式宣告特權方法
//在日誌窗體中新增一行,為輸入的內容進行簡單處理
myLogger.prototype={
write:function(message){
if(typeof message=='string' && message.length== message.length== message=='string' && message.length== message. ){
return this.writeRaw('沒有輸入資訊');
}
if(type遠this.writeRaw(message.toString()) ;
}else{
return this.writeRaw(typeof message);
}
} replace(/>/g,">");
return this.writeRaw(message);
},
header:function(message){
message=''+message+'';
return this.writeRaw(message);
}
};
window['LD']['log'] = new myLogger();LD.js[javascript] view plaincopyprint?// JavaScript Document
if(document.all && !document.getElementById){
p. ;
}
}
if(!String.repeat){
new Array(l+1).join(this);
}
}
if( !String.trim){
String.prototype.trim=function(){
}
}
(function(){
//構造命名空間
window['LD']={} ;
function $(){ ele 目前函數的參數數組。參數
for(var i=0;i
if(typeof element=='string'){
element=document.getElementById(element);
}
return element;
}
elements.push(element);
}
return elements;
}
function getElementsByClassName(className,tag){
parent=parent || document;
if(!(parent=$(parent))) return false;
//var allTags=document.getElementsByTagName(tag);
地
var allTags=(tag == "*" && parent.all) ? parent.all : parent.getElementsByTagName(tag); 🜠
className=className.replace(/-/g, "\-");
var regex=new RegExp("(^|\s)"+className+ "(\s|$ var element;
for(var i=0;i
}
}
return matchingElements;
}
window['LD']['getElementsByClassName']=getElementsByClassName;
return function(){
}
};
, [ 'LD']['bindFunction']=bindFunction;
function getBrowserWindowSize(){
var de=document.documentElement;
return{
'width':(
window.innerWidth
|| (de && de.clientWidth)
|| document.body.clientWidth),
'高度':(
window.innerHeight
|| (de && de.clientHeight)
|| de && document.body.clientHeight)
}
};
//註冊此事件
window['LD']['getBrowserWindowSize']=getBrowserWindowSize;
function addEvent(node,type,listener){
if(node.addEventListener){
node.addEventListener(type,listener,false);
返回 true;
節點['e'+type+listener]=listener;
ode.attachEvent('on'+type, node[type+listener]);
回傳 false;
};
//註冊此事件
window['LD']['addEvent']=addEvent;
})();
// JavaScript 文件
if(document.all && !document.getElementById){
document.getElementById=function(id){
return document.all[id];repe {
String.prototype.repeat=function(l){
return new Array(l+1).join(this);
}
}
返回this.replace(/^s+|+$/g,'');
}
}
(function(){
//建構命名空間
window['LD']={} ;
function $(){
var elements=new Array();
guments
for(var i=0;i
);
}
if(arguments.length==1){
return element;
}
空間
window['LD']['$ ']=$;
function getElementsByClassName(className,tag){
parent=parent || document;
if(!(parent s=document.getElementsByTagName (tag);
//對tag進行過濾,把tag的對象全取出來
var allTags=(tag == "*" && parent.all) ? parent.all : ingElement =new Array();
className=className.replace(/-/g,"\-");
var regex=new RegExp("(^|\s)"+className+ "(s|$)"( );
var element;
for(var i=0;i
}
}
//回中這個陣列
return=cingElements; yClassName;
function bindFunction(ojb,func){
return function() {
func.apply(obj,arguments);
}
};
window['LD']['bindFunction']=bind var de=document.documentElement;
return{
'width':(
window.innerWidth
|| (de && de.clientWidth)
window.innerHeight
|| (de && de.clientHeight)
|| de && document.body.clientHeight)
}
};
//註冊本事件
window['LD']['getBrowserWindowSize']=getBrowWrow window['LD']['getBrowserWindowSize']=getBrowLhmSmhk.
if(!(node=$(node))) return false;
if(node.addEventListener){
node.addEventListener"type,listener,false); (node.attachEvent){
node['e'+type+listener]=listener;
node[type+listener]=function(){node['e'+ '+type, node[type+listener]);
return true;
}
return false;
}) ();運作結果:
總結
中一個小符號點全部囊括,算是對JavaScript學習的一個小結。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

如何使用WebSocket和JavaScript實現線上語音辨識系統引言:隨著科技的不斷發展,語音辨識技術已成為了人工智慧領域的重要組成部分。而基於WebSocket和JavaScript實現的線上語音辨識系統,具備了低延遲、即時性和跨平台的特點,成為了廣泛應用的解決方案。本文將介紹如何使用WebSocket和JavaScript來實現線上語音辨識系

WebSocket與JavaScript:實現即時監控系統的關鍵技術引言:隨著互聯網技術的快速發展,即時監控系統在各個領域中得到了廣泛的應用。而實現即時監控的關鍵技術之一就是WebSocket與JavaScript的結合使用。本文將介紹WebSocket與JavaScript在即時監控系統中的應用,並給出程式碼範例,詳細解釋其實作原理。一、WebSocket技

如何利用JavaScript和WebSocket實現即時線上點餐系統介紹:隨著網路的普及和技術的進步,越來越多的餐廳開始提供線上點餐服務。為了實現即時線上點餐系統,我們可以利用JavaScript和WebSocket技術。 WebSocket是一種基於TCP協定的全雙工通訊協議,可實現客戶端與伺服器的即時雙向通訊。在即時線上點餐系統中,當使用者選擇菜餚並下訂單

如何使用WebSocket和JavaScript實現線上預約系統在當今數位化的時代,越來越多的業務和服務都需要提供線上預約功能。而實現一個高效、即時的線上預約系統是至關重要的。本文將介紹如何使用WebSocket和JavaScript來實作一個線上預約系統,並提供具體的程式碼範例。一、什麼是WebSocketWebSocket是一種在單一TCP連線上進行全雙工

JavaScript和WebSocket:打造高效的即時天氣預報系統引言:如今,天氣預報的準確性對於日常生活以及決策制定具有重要意義。隨著技術的發展,我們可以透過即時獲取天氣數據來提供更準確可靠的天氣預報。在本文中,我們將學習如何使用JavaScript和WebSocket技術,來建立一個高效的即時天氣預報系統。本文將透過具體的程式碼範例來展示實現的過程。 We

JavaScript教學:如何取得HTTP狀態碼,需要具體程式碼範例前言:在Web開發中,經常會涉及到與伺服器進行資料互動的場景。在與伺服器進行通訊時,我們經常需要取得傳回的HTTP狀態碼來判斷操作是否成功,並根據不同的狀態碼來進行對應的處理。本篇文章將教你如何使用JavaScript來取得HTTP狀態碼,並提供一些實用的程式碼範例。使用XMLHttpRequest

用法:在JavaScript中,insertBefore()方法用於在DOM樹中插入一個新的節點。這個方法需要兩個參數:要插入的新節點和參考節點(即新節點將要插入的位置的節點)。

JavaScript是一種廣泛應用於Web開發的程式語言,而WebSocket則是一種用於即時通訊的網路協定。結合二者的強大功能,我們可以打造一個高效率的即時影像處理系統。本文將介紹如何利用JavaScript和WebSocket來實作這個系統,並提供具體的程式碼範例。首先,我們需要明確指出即時影像處理系統的需求和目標。假設我們有一個攝影機設備,可以擷取即時的影像數
