亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript HTML DOM 事件監(jiān)聽器

addEventListener() 方法

addEventListener() 方法用於在指定元素中新增事件句柄。

addEventListener() 方法新增的事件句柄不會覆寫已存在的事件句柄。

你可以為一個元素新增多個事件句柄。

你可以在同一個元素中加入多個同類型的事件句柄,如:兩個 "click" 事件。

你可以為任何 DOM 物件新增事件監(jiān)聽,而不僅僅是 HTML 元素。如: window 物件。

addEventListener() 方法可以更簡單的控制事件(冒泡與捕獲)。

當(dāng)你使用 addEventListener() 方法時, JavaScript 從 HTML 標(biāo)記中分離開來,可讀性更強(qiáng), 在沒有控制HTML標(biāo)記時也可以加入事件監(jiān)聽。

你可以使用 removeEventListener() 方法來移除事件的監(jiān)聽。

語法

element.addEventListener(event, function, useCapture);

第一個參數(shù)是事件的類型(如"click" 或"mousedown").

第二個參數(shù)是事件觸發(fā)後呼叫的函數(shù)。

第三個參數(shù)是個布林值用來描述事件是冒泡還是捕獲。此參數(shù)是可選的。

注意:?不要使用 "on" 前綴。 例如,使用 "click" ,而不是使用 "onclick"。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<button id="myBtn">點擊</button>
<script>
document.getElementById("myBtn").addEventListener("click", function(){
    alert("Hello World!");
});
</script>
</body>
</html>

在同一個元素中新增多個事件句柄

addEventListener() 方法允許在同一個元素中新增多個事件,且不會覆寫已存在的事件:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<button id="myBtn">點擊查看</button>
<script>
var x = document.getElementById("myBtn");
x.addEventListener("click", myFunction);
x.addEventListener("click", someOtherFunction);
function myFunction() {
    alert ("Hello World!")
}
function someOtherFunction() {
    alert ("函數(shù)已執(zhí)行!")
}
</script>
</body>
</html>

為Window 物件新增事件句柄

addEventListener() 方法允許你在HTML DOM 物件新增事件監(jiān)聽, HTML DOM 物件如: HTML 元素, HTML 文件, window 物件。或其他支出的事件物件如: xmlHttpRequest 物件。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>嘗試重置瀏覽器的窗口觸發(fā) "resize" 事件句柄。</p>
<p id="demo"></p>
<script>
window.addEventListener("resize", function(){
    document.getElementById("demo").innerHTML = Math.random();
});
</script>
</body>
</html>

傳遞參數(shù)

當(dāng)傳遞參數(shù)值時,使用"匿名函數(shù)"呼叫帶參數(shù)的函數(shù):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<button id="myBtn">點擊查看結(jié)果</button>
<p id="demo"></p>
<script>
var p1 = 8;
var p2 = 8;
document.getElementById("myBtn").addEventListener("click", function() {
    myFunction(p1, p2);
});
function myFunction(a, b) {
    var result = a * b;
    document.getElementById("demo").innerHTML = result;
}
</script>
</body>
</html>

事件冒泡或事件捕獲?

事件傳遞有兩種方式:冒泡與捕獲。

事件傳遞定義了元素事件觸發(fā)的順序。 如果你將 <p> 元素插入 <div> 元素中,使用者點擊 <p> 元素, 哪個元素的 "click" 事件先被觸發(fā)呢?

在?冒泡?中,內(nèi)部元素的事件會先被觸發(fā),然後再觸發(fā)外部元素,即: <p> 元素的點擊事件先觸發(fā),然後觸發(fā)<div> 元素的點擊事件。

在?捕獲?中,外部元素的事件會先被觸發(fā),然後才會觸發(fā)內(nèi)部元素的事件,即: <div> 元素的點擊事件先觸發(fā),然後再觸發(fā)<p> 元素的點擊事件。

addEventListener() 方法可以指定"useCapture" 參數(shù)來設(shè)定傳遞類型:

addEventListener(event,?function,?useCapture);

預(yù)設(shè)值為false, 即冒泡傳遞,當(dāng)值為true 時, 事件使用捕獲傳遞。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
<style>
div {
    background-color: coral;
    border: 1px solid;
    padding: 50px;
}
</style>
</head>
<body>
<div id="myDiv">
<p id="myP">點擊段落,我是冒泡。</p>
</div><br>
<div id="myDiv2">
<p id="myP2">點擊段落,我是捕獲。 </p>
</div>
<script>
document.getElementById("myP").addEventListener("click", function() {
    alert("你點擊了 P1 元素!");
}, false);
document.getElementById("myDiv").addEventListener("click", function() {
    alert(" 你點擊了 DIV1 元素 !");
}, false);
document.getElementById("myP2").addEventListener("click", function() {
    alert("你點擊了 P2 元素!");
}, true);
document.getElementById("myDiv2").addEventListener("click", function() {
    alert("你點擊了 DIV2 元素 !");
}, true);
</script>
</body>
</html>

removeEventListener() 方法

removeEventListener() 方法移除由 addEventListener() 方法新增的事件句柄:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<head>
<style>
#myDIV {
    background-color: coral;
    border: 1px solid;
    padding: 50px;
    color: white;
}
</style>
</head>
<body>
<div id="myDIV"> div 元素添加了 onmousemove 事件句柄,鼠標(biāo)在桔紅色的框內(nèi)移動時會顯示隨機(jī)數(shù)。
  <p>點擊按鈕移除 DIV 的事件句柄。</p>
  <button onclick="removeHandler()" id="myBtn">點擊暫停</button>
</div>
<p id="demo"></p>
<script>
document.getElementById("myDIV").addEventListener("mousemove", myFunction);
function myFunction() {
    document.getElementById("demo").innerHTML = Math.random();
}
function removeHandler() {
    document.getElementById("myDIV").removeEventListener("mousemove", myFunction);
}
</script>
</body>
</html>


#
繼續(xù)學(xué)習(xí)
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style type="text/css"> .item{ display: table; margin: 100px; } #outer{ width:400px; height:400px; background-color: blue; } #inner{ vertical-align: middle; width:200px; height:200px; background-color: white; } #core{ width:80px; height:80px; background-color: red; text-indent: 10px; line-height: 50px; } </style> </head> <body> <div class='item' id='outer' onclick="alert('outer')"> <div class='item' id='inner' onclick="alert('inner');stopbubble(arguments[0])"> <div class='item' id='core' onclick="alert('core')"> core!!!!! </div> </div> </div> <p>點擊不同的顏色框</p> </body> <!--you must write <script> under <body>--> <script type=‘text/javascript‘> var core = document.getElementById(‘core‘); core.addEventListener("click",function(){alert(‘dddddd‘)},false); //addEventListener: can add one more event to node "core" function stopbubble(e){ e.stopPropagation();//stop bubble event on this node } </script> </html>
提交重置程式碼