首先,在JavaScript的事件模型中,你会来
跨越一个称为事件冒泡的概念
(这使得一个事件从 child 传播
元素到父元素)。为了避免
这样的冒泡效果,很多开发者
使用名为 stopPropagation( )
的事件方法。
或者,开发人员已经开始使用
return false
语句来停止这种传播。
现在,还有一个术语叫做
preventDefault()
。顾名思义,这
方法防止任何默认行为
要触发的元素。最佳用例是防止
用于打开链接的 anchor 标记。
你可能会遇到这样的场景
想防止 anchor 标记
打开链接(默认行为)以及停止
从上升到 parent 的事件。
在这种情况下,与其写两行代码,
您可以单行完成,即; 返回错误
返回错误;
return false;
调用它时会做 3 件独立的事情:
event.preventDefault()
– 它停止浏览器的默认行为。
event.stopPropagation()
– 它阻止事件传播(或“冒泡”)DOM。
- 停止回调执行并在调用时立即返回。
请注意,此行为不同于普通(非 jQuery)事件处理程序,值得注意的是,在后者中,return false
不会阻止事件冒泡。
preventDefault();
preventDefault();
做一件事:停止浏览器的默认行为。
什么时候使用它们?
我们知道它们的作用,但何时使用它们?简单地说,这取决于你想要完成什么。如果你想“只是”阻止默认的浏览器行为,请使用 preventDefault();
。使用返回假;当您想阻止默认浏览器行为并阻止事件传播 DOM 时。在大多数情况下,您会使用 return false;你真正想要的是 preventDefault()
。
示例:
让我们尝试通过示例来理解:
我们将看到纯 JAVASCRIPT 示例
示例 1:
<div onclick='executeParent()'>
<a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
function executeChild() {
alert('Link Clicked');
}
function executeParent() {
alert('div Clicked');
}
</script>
Run the above code you will see the hyperlink ‘Click here to visit
stackoverflow.com‘ now if you click on that link first you will get
the javascript alert Link Clicked Next you will get the javascript
alert div Clicked and immediately you will be redirected to
stackoverflow.com.
示例 2:
<div onclick='executeParent()'>
<a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
function executeChild() {
event.preventDefault();
event.currentTarget.innerHTML = 'Click event prevented'
alert('Link Clicked');
}
function executeParent() {
alert('div Clicked');
}
</script>
Run the above code you will see the hyperlink ‘Click here to visit
stackoverflow.com‘ now if you click on that link first you will get
the javascript alert Link Clicked Next you will get the javascript
alert div Clicked Next you will see the hyperlink ‘Click here to
visit stackoverflow.com‘ replaced by the text ‘Click event prevented‘
and you will not be redirected to stackoverflow.com. This is due > to event.preventDefault() method we used to prevent the default click
action to be triggered.
示例 3:
<div onclick='executeParent()'>
<a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
function executeChild() {
event.stopPropagation();
event.currentTarget.innerHTML = 'Click event is going to be executed'
alert('Link Clicked');
}
function executeParent() {
alert('div Clicked');
}
</script>
This time if you click on Link the function executeParent() will not
be called and you will not get the javascript alert div Clicked
this time. This is due to us having prevented the propagation to the
parent div using event.stopPropagation() method. Next you will see the
hyperlink ‘Click here to visit stackoverflow.com‘ replaced by the text
‘Click event is going to be executed‘ and immediately you will be
redirected to stackoverflow.com. This is because we haven’t prevented
the default click action from triggering this time using
event.preventDefault() method.
示例 4:
<div onclick='executeParent()'>
<a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
function executeChild() {
event.preventDefault();
event.stopPropagation();
event.currentTarget.innerHTML = 'Click event prevented'
alert('Link Clicked');
}
function executeParent() {
alert('Div Clicked');
}
</script>
If you click on the Link, the function executeParent() will not be
called and you will not get the javascript alert. This is due to us
having prevented the propagation to the parent div using
event.stopPropagation() method. Next you will see the hyperlink ‘Click
here to visit stackoverflow.com‘ replaced by the text ‘Click event
prevented‘ and you will not be redirected to stackoverflow.com. This
is because we have prevented the default click action from triggering
this time using event.preventDefault() method.
示例 5:
For return false I have three examples and all appear to be doing the exact same thing (just returning false), but in reality the
results are quite different. Here's what actually happens in each of
the above.
案例:
- 从内联事件处理程序返回 false 会阻止浏览器导航到链接地址,但不会阻止事件通过 DOM 传播。
- 从 jQuery 事件处理程序返回 false 会阻止浏览器导航到链接地址,并阻止事件通过 DOM 传播。
- 从常规 DOM 事件处理程序返回 false 绝对没有任何作用。
将看到所有三个示例。
- 内联返回 false。
<div onclick='executeParent()'>
<a href='http://stackoverflow.com' onclick='return false'>Click here to visit stackoverflow.com</a>
</div>
<script>
var link = document.querySelector('a');
link.addEventListener('click', function() {
event.currentTarget.innerHTML = 'Click event prevented using inline html'
alert('Link Clicked');
});
function executeParent() {
alert('Div Clicked');
}
</script>
- 从 jQuery 事件处理程序返回 false。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<a href='http://stackoverflow.com'>Click here to visit stackoverflow.com</a>
</div>
<script>
$('a').click(function(event) {
alert('Link Clicked');
$('a').text('Click event prevented using return FALSE');
$('a').contents().unwrap();
return false;
});
$('div').click(function(event) {
alert('Div clicked');
});
</script>
- 从常规 DOM 事件处理程序返回 false。
<div onclick='executeParent()'>
<a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
function executeChild() {
event.currentTarget.innerHTML = 'Click event prevented'
alert('Link Clicked');
return false
}
function executeParent() {
alert('Div Clicked');
}
</script>
希望这些例子很清楚。尝试在一个 html 文件中执行所有这些示例,看看它们是如何工作的。