<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[阻止jQuery事件冒泡学习]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 28 Feb 2012 02:18:31 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	近来看一个Js函数如下：<br/><textarea name="code" class="js" rows="15" cols="100">
$(&quot;#addon-script-2&quot;).click(function(e)&#123;
&nbsp;&nbsp;eventstop(e);
&nbsp;&nbsp;alert(&quot;你的权限不足&quot;);
&nbsp;&nbsp;return false;
&#125;);&nbsp;&nbsp;
</textarea><br/>对这个eventstop(e);,有点迷惑<br/><textarea name="code" class="JS" rows="15" cols="100">
var eventstop = function(e)&#123;
&nbsp;&nbsp;if(e&amp;&amp;e.stopPropagation)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;e.stopPropagation();
&nbsp;&nbsp;&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;window.event.cancelBubble=true; 
&nbsp;&nbsp;&#125;
&#125;
</textarea><br/>Demo:<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;JQuery 阻止事件冒泡方法&lt;/title&gt;
&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;&nbsp;&nbsp;
&lt;script&gt;!window.jQuery &amp;&amp; document.write(&#039;&lt;script src=&quot;js/jquery-1.4.2.min.js&quot;&gt;&lt;&#92;/script&gt;&#039;)&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/script&gt;&nbsp;&nbsp;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;DIV&lt;/div&gt;
&lt;span&gt;SPAN&lt;/span&gt;
&lt;p&gt;P&lt;/p&gt;
&lt;script&gt;
$(&#039;div&#039;).click(function(event)
&#123;
&nbsp;&nbsp; alert(&#039;div&#039;);
&nbsp;&nbsp; event.stopPropagation();//加上这一句防止继续向document进行传递：JQUERY事件是按由里向外的处理顺序，这就是事件冒泡。 否则会向上传递到：alert(&quot;document&quot;);
&#125;);
$(document).click(function()
&#123;
&nbsp;&nbsp; alert(&#039;document&#039;);
&#125;);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;


</textarea><br/><br/><br/>近来发现比如页面点击位置监控，目前有用坐标的，但会因为屏幕的大小导致不一致，可能不是很准，用如下直接监控id就好很多，<br/>再就是一个div里有很多个button，或者其他div，如果一个一个的去写它的调用函数，这样会很麻烦，于是这个冒泡的思想也就出来了，如下（注意在：IE和Firefox下不是特别一样）：<br/><textarea name="code" class="JS" rows="15" cols="100">
&lt;html&gt;
&lt;head&gt;
&nbsp;&nbsp;&lt;style&gt;
&nbsp;&nbsp;#buttondiv&#123;
&nbsp;&nbsp;&nbsp;&nbsp;background:yellow;
&nbsp;&nbsp;&nbsp;&nbsp;padding:20px;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&lt;/style&gt;
&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;&nbsp;&nbsp;
&lt;script&gt;!window.jQuery &amp;&amp; document.write(&#039;&lt;script src=&quot;js/jquery-1.4.2.min.js&quot;&gt;&lt;&#92;/script&gt;&#039;)&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/script&gt;&nbsp;&nbsp;

&lt;/head&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.js&quot;&gt;
&lt;/script&gt;
&lt;div id=&quot;buttondiv&quot;&gt;
 &lt;button id=&quot;1&quot;&gt;1&lt;/button&gt;
 &lt;button id=&quot;2&quot;&gt;2&lt;/button&gt;
 &lt;button id=&quot;3&quot;&gt;3&lt;/button&gt;
&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;

$(&quot;#buttondiv&quot;).click(function(e)&#123;
&nbsp;&nbsp;var e = e&#124;&#124;window.event;
&nbsp;&nbsp;var src = e.target&#124;&#124;e.srcElement;
&nbsp;&nbsp;//console.log(src); //IE下是另外一个：e，而在firefox下是src&nbsp;&nbsp;
&#125;)



&lt;/script&gt;
&lt;/html&gt;

</textarea><br/><br/><br/>Js兄弟说是让我查找了一下事件冒泡，这个文章写得好，能解惑：http://justcoding.iteye.com/blog/587876<br/>Query对DOM的事件触发具有冒泡特性。有时利用这一特性可以减少重复代码，但有时候我们又不希望事件冒泡。这个时候就要阻止 jQuery.Event冒泡。<br/> <br/>在jQuery.Event 的文档 中的开头得知，jQuery.Event对象是符合W3C标准的一个事件对象，同时jQuery.Event免去了检查兼容IE的步骤。<br/> <br/>jQuery.Event提供了一个非常简单的方法来阻止事件冒泡：event.stopPropagation();<br/><br/><textarea name="code" class="JS" rows="15" cols="100">
$(&quot;p&quot;).click(function(event)&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;event.stopPropagation(); // do something&nbsp;&nbsp;
&#125;) 
</textarea><br/><br/>但是这个方法对使用live 绑 定的事件没有作用，需要一个更简单的方法阻止事件冒泡：return false;<br/><textarea name="code" class="JS" rows="15" cols="100">
$(&quot;p&quot;).live(&quot;click&quot;, function()&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$(this).after(&quot;Another paragraph!&quot;);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;return false;&nbsp;&nbsp;
&#125;); 
</textarea><br/><br/>另外 JavaScript 阻止冒泡<br/> <br/>　阻止冒泡事件的方法有两种，第一种是IE的方法，第二种是DOM方法，至于为什么要分为两种方法去讨论之，这里就不做讨论了，总之是浏览器一些蹩 脚的问题，好了废话不说了，直接贴代码了：<br/><br/><textarea name="code" class="JS" rows="15" cols="100">
//阻止冒泡事件&nbsp;&nbsp;
 function stopBubble(e) &#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; if (e &amp;&amp; e.stopPropagation) &#123;//非IE&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.stopPropagation();&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; &#125;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; else &#123;//IE&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; window.event.cancelBubble = true;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; &#125;&nbsp;&nbsp;
 &#125;&nbsp;&nbsp;
</textarea><br/><br/>举个例子：就是谷歌首页的更多选项吧，不知道的可以去看看http://www.google.com/ 。<br/>这里要应用两个点击事件，一个是div.onclick，另一个就是document.onclick，那么问题就出现了，在调用 div.onclick的时候，由于冒泡事件的存在，会自动的调用document.onclick，由于冒泡的顺序是从里向外的<br/>（div-&gt;body-&gt;document-&gt;html）所以div.onclick事件就会被覆盖掉，也就不会执行了，解决的方案其 实也是很简单的哦，就是在执行div.onclick的时候阻止冒泡事件就好了，那怎么阻止呢，就调用上面的函数就ok了！哈哈！ <br/>ps：在介绍 一下阻止浏览器默认行为的方法，大同小异，这里就不再赘述了。<br/>正是这个函数，如下：<br/><textarea name="code" class="JS" rows="15" cols="100">
function stopDefault(e) &#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; //阻止默认浏览器动作(W3C)&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; if (e &amp;&amp; e.preventDefault)&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.preventDefault();&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; //IE中阻止函数器默认动作的方式&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; window.event.returnValue = false;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; return false;&nbsp;&nbsp;
 &#125;
</textarea><br/>Tags - <a href="http://jackxiang.com/tags/%25E5%2587%25BD%25E6%2595%25B0/" rel="tag">函数</a>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 阻止jQuery事件冒泡学习]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>