1// prevent bubbling effect
2function DownloadAsset(AssetId, e) {
3
4 if (!e) var e = window.event
5 e.cancelBubble = true;
6 if (e.stopPropagation) e.stopPropagation();
7
8 // your ajax call
9 $.ajax({....})
10}
1var logElement = document.getElementById('log');
2
3function log(msg) {
4 logElement.innerHTML += ('<p>' + msg + '</p>');
5}
6
7function capture() {
8 log('capture: ' + this.firstChild.nodeValue.trim());
9}
10
11function bubble() {
12 log('bubble: ' + this.firstChild.nodeValue.trim());
13}
14
15function clearOutput() {
16 logElement.innerHTML = "";
17}
18
19var divs = document.getElementsByTagName('div');
20for (var i = 0; i < divs.length; i++) {
21 divs[i].addEventListener('click', capture, true);
22 divs[i].addEventListener('click', bubble, false);
23}
24var clearButton = document.getElementById('clear');
25clearButton.addEventListener('click', clearOutput);
1p {
2 line-height: 0;
3}
4
5div {
6 display:inline-block;
7 padding: 5px;
8
9 background: #fff;
10 border: 1px solid #aaa;
11 cursor: pointer;
12}
13
14div:hover {
15 border: 1px solid #faa;
16 background: #fdd;
17}
1<div>1
2 <div>2
3 <div>3
4 <div>4
5 <div>5</div>
6 </div>
7 </div>
8 </div>
9</div>
10<button id="clear">clear output</button>
11<section id="log"></section>
1<script>
2 form.onclick = function(event) {
3 event.target.style.backgroundColor = 'yellow';
4
5 // chrome needs some time to paint yellow
6 setTimeout(() => {
7 alert("target = " + event.target.tagName + ", this=" + this.tagName);
8 event.target.style.backgroundColor = ''
9 }, 0);
10};
11</script>
12<html>
13<head>
14 <meta charset="utf-8">
15 <link rel="stylesheet" href="example.css">
16</head>
17
18<body>
19 A click shows both <code>event.target</code> and <code>this</code> to compare:
20
21 <form id="form">FORM
22 <div>DIV
23 <p>P</p>
24 </div>
25 </form>
26</body>
27</html>
28
29<!-- A handler on a parent element can always get the details about where it actually happened.
30
31The most deeply nested element that caused the event is called a target element, accessible as event.target.
32
33Note the differences from this (=event.currentTarget):
34
35event.target – is the “target” element that initiated the event, it doesn’t change through the bubbling process.
36this – is the “current” element, the one that has a currently running handler on it.
37For instance, if we have a single handler form.onclick, then it can “catch” all clicks inside the form. No matter where the click happened, it bubbles up to <form> and runs the handler.
38
39In form.onclick handler:
40
41this (=event.currentTarget) is the <form> element, because the handler runs on it.
42event.target is the actual element inside the form that was clicked. -->