####一、addEventListener的使用方式:
>target.addEventListener(type, listener, useCapture);
>target: 文檔節點、document、window 或 XMLHttpRequest。
type: 字符串,事件名稱,不含“on”,比如“click”、“mouseover”、“keydown”等。
listener :實現了 EventListener 接口或者是 JavaScript 中的函數。
useCapture :是否使用捕捉,一般用 false 。例如:document.getElementById("testText").addEventListener("keydown", function (event) { alert(event.keyCode); }, false);
######格式
*element*.addEventListener(*event*,?*function*,?*useCapture*)
|參數 |描述|
| :-------- | --------:| :--: |
|*event*|必須。字符串,指定事件名。**注意:**?不要使用 "on" 前綴。 例如,使用 "click" ,而不是使用 "onclick"。?**提示:**?所有 HTML DOM 事件,可以查看我們完整的?[HTML DOM Event 對象參考手冊](http://www.runoob.com/jsref/dom-obj-event.html)。
|*function*|必須。指定要事件觸發時執行的函數。?當事件對象會作為第一個參數傳入函數。 事件對象的類型取決于特定的事件。例如, "click" 事件屬于MouseEvent(鼠標事件) 對象。|
|*useCapture*|可選。布爾值,指定事件是否在捕獲或冒泡階段執行??赡苤?true - 事件句柄在捕獲階段執行false- false- 默認。事件句柄在冒泡階段執行|
####二、IE中:
>target.attachEvent(type, listener);
target: 文檔節點、document、window 或 XMLHttpRequest。
type: 字符串,事件名稱,含“on”,比如“onclick”、“onmouseover”、“onkeydown”等。
listener :實現了 EventListener 接口或者是 JavaScript 中的函數。 例如:document.getElementById("txt").attachEvent("onclick",function(event){alert(event.keyCode);});
>W3C 及 IE 同時支持移除指定的事件, 用途是移除設定的事件, 格式分別如下:
>>#####W3C格式:
>>removeEventListener(event,function,capture/bubble);
>>#####Windows IE的格式如下:
>>detachEvent(event,function);
####三、Javascript獲取瀏覽器地址欄url
####1, window.location.href
整個URl字符串(在瀏覽器中就是完整的地址欄)
本例返回值: http://www.x2y2.com:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere
####2,window.location.protocol
URL 的協議部分
本例返回值:http:
####3,window.location.host
URL 的主機部分
本例返回值:www.x2y2.com
####4,window.location.port
URL 的端口部分
如果采用默認的80端口(update:即使添加了:80),那么返回值并不是默認的80而是空字符
本例返回值:""
####5,window.location.pathname
URL 的路徑部分(就是文件地址)
本例返回值:/fisker/post/0703/window.location.html
####6,window.location.search
查詢(參數)部分
除了給動態語言賦值以外,我們同樣可以給靜態頁面,并使用javascript來獲得相信應的參數值
本例返回值:?ver=1.0&id=6
####7,window.location.hash
錨點
本例返回值:#imhere