1、遇到的问题。
这是一个Html网页,很简单。
代码-1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Abraham Lincoln's Gettysburg Address</title> <link rel="stylesheet" href="04.css" type="text/css" /> <script src="jquery.js"></script> <script src="04.js"></script> </head> <body> <div id="container"> <h2>Abraham Lincoln's Gettysburg Address</h2> <div id="switcher"> <div class="label">Text Size</div> <button id="switcher-default">Default</button> <button id="switcher-large">Bigger</button> <button id="switcher-small">Smaller</button> </div> <div class="speech"> <p>Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p> <p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting-place for those who here gave their lives that the nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow, this ground.</p> <a href="www.baidu.com" class="more">read more</a> <p>The brave men, living and dead, who struggled here have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember, what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.</p> <p>It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom and that government of the people, by the people, for the people, shall not perish from the earth.</p> </div> </div> </body> </html>
代码-2
$(document).ready(function(){ $('p').eq(1).hide(); $('a.more').click(function(){ $('p').eq(1).show('slow'); $(this).hide(); return false; }); });
代码-2中的JS代码很简单,就是让第二个的<P/>元素中的内容先隐藏,点击锚元素(就是<a/>元素),则将隐藏的内容展示出来。
但是在JS代码的最后,有一句:
return false;
的代码。该代码的作用就是去掉锚元素的默认行为。
2、分析
在jQuery中return false其本质是:抑制了元素的冒泡事件,及抑制了元素的默认行为。
说得直白点:
“return false”=“stopPropagation()”+“preventDefault()”;
即:在方法的最后使用了return false,就相当于对事件对象同时调用了stopPropagation()和preventDefault()。
【注】:stopPropagation()是阻止事件冒泡方法,
preventDefault()是阻止元素的默认行为的方法。
所以,代码-2的JS代码实际上可以这样写:
$(document).ready(function(){ $('p').eq(1).hide(); $('a.more').click(function(eventObject){ $('p').eq(1).show('slow'); $(this).hide(); eventObject.stopPropagation(); eventObject.preventDefault(); }); });
相关推荐
JQUERY THE RETURN FLIGHT
jquery.returnTop返回顶部Jquery插件
可能在你刚开始学习关于jQuery事件处理时,看到的第一个例子就是关于如何阻止浏览器执行默认行为,比如下面这段演示click事件的代码: 代码如下:$(“a.toggle”).click(function () { $(“#mydiv”).toggle();...
Jquery的each里面用return false代替break;return ture 代替continue
深入理解JavaScript系列,超实用的jQuery代码段,jQuery源码分析系列。
本压缩包内包含以下文件: jquery-1.12.4.js jquery-1.12.4.min.js jQuery1.11.0_20140330.chm jQueryAPI_CHM.CHM
原生js使用forEach()与jquery使用each()遍历数组,return false 的区别: 1、使用each()遍历数组a,如下: var a=[20,21,22,23,24]; $.each(a, function(index,val) { console.log('index='+index);\nif(index==2)...
jQuery是一个JavaScript框架,自面世以来,以其快速、简洁,能够很轻易地处理HTML文档、控制事件、给页面添加动画和Ajax效果等功能使多很多WEB编程者对其非常热爱,本手册旨在帮助广大jQuery爱好者快速了解jquery库...
jQuery API 1.4.4 中文参考手册.chm,jquery api 下载,jQuery 1.4.4 API 参考文档中文版.rar,jQuery最新版CHM下载,jQuery中文参考手册,jQuery中文版1.4.4参考手册,jQuery 1.4.4 速查表,jQuery中文参考手册,jQuery中文...
return false; } var p=document.getelementbyid('ddlparentId'); if(document.getelementbyId('hidcateId').value==p.options[p.selectedIndex].value) { alert('不能选择自己作为父类!'); return false; } return...
jquery1.8.0 和jquery 1.8 中文 api
NULL 博文链接:https://qinchaoyong.iteye.com/blog/517516
jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)...
主要介绍了jQuey中的return false作用,以及解决jquery中的return false不起作用的方法,感兴趣的小伙伴们可以参考一下
Code School jQuery: The Return Flight slide
jQuery最新中文文档,最实用,最靠谱
jquery jquery下载jquery下载jquery下载
jquery 精简版 jquery 精简版 jquery 精简版jquery 精简版 jquery 精简版 jquery 精简版 jquery 精简版
jquery ajax return没有返回值的解决方法,需要的朋友可以参考下。