`
南山忍者
  • 浏览: 83501 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

对jQuery中的return false的理解

阅读更多

 

      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&mdash;that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion&mdash;that we here highly resolve that these dead shall not have died in vain&mdash;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();
	});
});

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics