亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么點擊文本框聚焦會彈出窗口?focus不是不冒泡么?求解~

<!DOCTYPE html>

<html>


<head>

? ? <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

? ? <title></title>

? ? <style>

? ? ? ? .left div,

? ? ? ? .right div {

? ? ? ? ? ? width: 500px;

? ? ? ? ? ? height: 50px;

? ? ? ? ? ? padding: 5px;

? ? ? ? ? ? margin: 5px;

? ? ? ? ? ? float: left;

? ? ? ? ? ? border: 1px solid #ccc;

? ? ? ? }


? ? ? ? .left div {

? ? ? ? ? ? background: #bbffaa;

? ? ? ? }


? ? ? ? .right div {

? ? ? ? ? ? background: yellow;

? ? ? ? }

? ? </style>

? ? <script src="http://www.xianlaiwan.cn/static/lib/jquery/1.9.1/jquery.js"></script>

</head>


<body>

<h2>自定義事件triggerHandler</h2>

<div class="left">

? ? <div id="accident">

? ? ? ? <a>triggerHandler事件</a>

? ? ? ? <input type="text">

? ? </div>

? ? <button>事件冒泡,觸發瀏覽器默認聚焦行為</button><br><br>

? ? <button>不會冒泡,不觸發瀏覽器默認聚焦行為</button>

</div>

<script type="text/javascript">


? ? //給input綁定一個聚焦事件

? ? $("input").on("focus",function(event,title) {

? ? ? ? $(this).val(title)

? ? });


? ? $("#accident").on("click",function() {

? ? ? ? alert("trigger觸發的事件會在 DOM 樹中向上冒泡");

? ? });

? ? //trigger觸發focus

? ? $("button:first").click(function() {

? ? ? ? $("a").trigger("click");

? ? ? ? $("input").trigger("focus");

? ? });


? ? //triggerHandler觸發focus

? ? $("button:last").click(function() {

? ? ? ? $("a").triggerHandler("click");

? ? ? ? $("input").triggerHandler("focus","沒有觸發默認聚焦事件");

? ? });




</script>

</body>


</html>


正在回答

1 回答

不是input元素上的focus冒泡,是通過trigger模擬觸發了a標簽的點擊事件,點擊事件向上冒泡

?$("button:first").click(function() {

? ? ? ? $("a").trigger("click");//這兒會向上冒泡至父級元素,雖然a標簽并沒有顯示綁定事件處理函數

? ? ? ? $("input").trigger("focus");

? ? });


2 回復 有任何疑惑可以回復我~
#1

三千道藏

看見評論又說input點擊出現alert,借此地說一下:并不是觸發了input事件冒泡,而是觸發click事件委托。
2019-01-20 回復 有任何疑惑可以回復我~
#2

三千道藏 回復 三千道藏

<div> <input type="text"> </div> <script> $('div').on('click',function(e){ console.log('ok',e) }) // $('input').on('focus',function(e){ // console.log('ok',e) // }) </script>
2019-01-20 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么點擊文本框聚焦會彈出窗口?focus不是不冒泡么?求解~

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號