事件冒泡。阻止事件冒泡 |阻止元素的預設行為,冒泡事件

來源:互聯網
上載者:User

事件冒泡。阻止事件冒泡 |阻止元素的預設行為,冒泡事件

<.1>

如果在頁面中重疊了多個元素,並且重疊的這些元素都綁定了同一個事件,那麼就會出
現冒泡問題。 

觸發的順序是從小範圍到大範圍。
這就是所謂的冒泡現象,一層一層往上。

<html><head>    <title></title>    <script src="jquery-2.1.3.js"></script>    <style type="text/css">        div#a {            width: 500px;            height: 500px;            background-color: red;        }        div#b,div#d{            width: 300px;            height: 300px;            background-color: blue;        }        div#c,div#e{            width: 150px;            height: 150px;            background-color: yellow;        }    </style></head><body>    <!--阻止事件冒泡-->    <div id="a">        <div id="b">            <div id="c">                <a id="a_linkB" href="http:///www.hao123.com" target="_blank">好123導航</a>            </div>        </div>    </div>    <!--阻止事件的預設行為-->    <a id="a_linkA" href="http://www/baidu.com" target="_blank">百度</a>    <!--阻止表單提交-->    <form action="123.html">        <input id="sub" type="submit" value="提交" />    </form>    <!--阻止事件冒泡,並阻止該事件後的後續事件處理函數-->    <div id="d">        <div id="e">測試stopImmediatePropagation()取消事件冒泡,並取消該事件的後續事件處理函數</div>    </div></body></html>
<script type="text/javascript">    $(function () {                //--------------------------【1】stopPropagation() 禁止事件冒泡        $("div").click(function (e) {            //e.stopPropagation(); //禁止div元素繫結的click事件冒泡 。當然我們還可以禁止其他事件的冒泡,比如mouseOver..等等        })        $("#a").click(function () {            alert("我是A");        })        $("#b").click(function () {            alert("我是B");        })        $("#c").click(function (e) {           // e.stopPropagation(); //禁止id=c的div元素繫結的click事件冒泡            alert("我是C");        })        //-----------------------【2】preventDefault() 取消某個元素的預設行為        //網頁中的元素,在操作的時候會有自己的預設行為。比如:右擊文字框輸入地區,會彈出系統功能表、點擊超連結會跳轉到指定頁面、點擊提交按鈕會提交資料。這就是元素的預設行為。        $("#a_linkA").click(function (e) {            e.preventDefault(); //阻止id為a_link的這個超連結的預設行為,當你點擊這個超連結的時候,就不會在進行跳轉到指定的網頁了。        })        //-------------------------------------禁止表單提交        $("#sub").click(function (e) {            e.preventDefault();//禁止表單提交 ;   禁止submit的預設提交行為。我們看到點擊“提交”的時不再提交資料了。        })        //如果說你不想對submit按鈕的click預設事件做阻止,(submit的click預設事件就是提交表單),而是僅僅是想阻止這個提交事件,我們就可以在form表單這進行        $("form").submit(function (e) {            e.preventDefault(); //禁止表單提交。推薦用法        });                //-------------------------------------同時阻止預設行為且禁止冒泡行為        $("#a_linkB").click(function (e) {            e.stopPropagation(); //禁止id=a_linkB這個<a>標籤的click事件冒泡            e.preventDefault();  //禁止id=a_linkB這個<a>標籤的click事件的預設行為(即:點擊超連結不再有跳轉的功能了)            //。這兩個方法如果需要同時啟用的時候 還有一種簡寫方案代替,就是直接return false。            //return false;        });        $("#a_linkB").click(function (e) {            alert(e.isDefaultPrevented()); //判斷是否調用了preventDefault()方法 。即:判斷是否阻止了#a_linkB元素的click事件的預設行為  列印出:true            alert(e.isPropagationStopped()); // 判斷是否調用了stopPropagation()方法。即:判斷是否阻止了#a_linkB元素的click事件冒泡 //列印出:true        })        //--------------------------【3】stopImmediatePropagation() 取消事件冒泡,並取消該事件的後續事件處理函數        $("#d").click(function () {            alert("我是D");  //測試冒泡        })        $("#e").click(function (e) {            e.stopImmediatePropagation(); //當我使用了這段代碼後,下面那段$("#e").click(function () { alert("我還是E") }) 代碼就不會再執行了。 而且也不會再執行$("#d").click(function () { alert("我是D");}) 這段代碼了。因為stopImmediatePropagation()的作用就是阻止事件冒泡,【所以它就阻止了$("#d").click(function () { alert("我是D");})這段代碼的執行】。  並且阻止事件的後續的處理函數【而下面那段$("#e").click(function () { alert("我還是E") }) 代碼就是當前事件的後續事件。所以就又被阻止了】            alert("我是E");            alert(e.isImmediatePropagationStopped());//判斷是否調用了stopImmediatePropagation()方法        })        $("#e").click(function (e) { //如果上面的那個函數使用了stopImmediatePropagation();方法,我將不會被執行到。(注意:我們都是#e元素的click事件哦,如果不是同的元素是不起作用的)             alert("我還是E")                   })    })</script>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.