When to use PreventDefault( ) vs Return false?
首先,在javascript的事件模型中,您将通过一个称为"事件冒泡"的概念(使事件从子级传播)元素到父元素)。为了避免这种冒泡效应,很多开发者使用名为
你可能会遇到这样的情况希望阻止定位标记打开链接(默认行为)并停止从上到父级的事件。在这种情况下,不是写两行代码,你可以单列完成,即:
返回错误;
当你称之为
请注意,此行为与普通(非jquery)事件处理程序不同,在正常(非jquery)事件处理程序中,
preventDefault();
何时使用它们?
我们知道他们做什么,但什么时候使用他们?简单地说,这取决于你想完成什么。如果要"仅"阻止默认浏览器行为,请使用
实例:
让我们试着用例子来理解:
我们将看到纯javascript示例
例1:
1 2 3 4 5 6 7 8 9 10 | Click here to visit stackoverflow.com function executeChild() { alert('Link Clicked'); } function executeParent() { alert('div Clicked'); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 | Click here to visit stackoverflow.com function executeChild() { event.preventDefault(); event.currentTarget.innerHTML = 'Click event prevented' alert('Link Clicked'); } function executeParent() { alert('div Clicked'); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 | Click here to visit stackoverflow.com function executeChild() { event.stopPropagation(); event.currentTarget.innerHTML = 'Click event is going to be executed' alert('Link Clicked'); } function executeParent() { alert('div Clicked'); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Click here to visit stackoverflow.com function executeChild() { event.preventDefault(); event.stopPropagation(); event.currentTarget.innerHTML = 'Click event prevented' alert('Link Clicked'); } function executeParent() { alert('Div Clicked'); } |
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.
案例:
将看到这三个例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Click here to visit stackoverflow.com 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'); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> Click here to visit stackoverflow.com $('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'); }); |
1 2 3 4 5 6 7 8 9 10 11 12 | Click here to visit stackoverflow.com function executeChild() { event.currentTarget.innerHTML = 'Click event prevented' alert('Link Clicked'); return false } function executeParent() { alert('Div Clicked'); } |
希望这些例子是清楚的。尝试在HTML文件中执行所有这些示例,以了解它们是如何工作的。