Ajax 板


LINE

對 你的問題在於你重複bind event 你的event trigger時都會把每個bind過event handler都執行一次 這是DOM2 event的特性 當你把他移出來之後 你可能會有另一個問題就是如何pass你的 box_title 此時你可以把他宣告在外面 而你mousedown時 可以去改變的他的值 所以你的input的click trigger時 因為兩個closures在同個environment 所以應該可以抓到每次的box_title改變後的值 試試看吧 ※ 引述《iam87king (沉默傷心)》之銘言: : ※ 引述《terrybob (罪雲樵)》之銘言: : : [問題描述] : : 當點擊<a>時,會呼叫<div id="shareit-box">顯示在頁面上, : : 當點擊<div id="shareit-box">裡的按鈕,進行關閉動作時, : : 我放置了 alert("結束"+boxtitle); ,看關閉按鈕執行時,跳了幾次alert… : : 測試結果時,會一直不斷跳出alert,而且跳出alert的順序, : : 會是我每次點過<a>的連結順序… : : 我想問的是…是否有方法,可以第一次點擊顯示alert之後, : : 該物件執行就結束了,其他的alert不再會執行? : : 謝謝! : : [html] : : <div style="border:#000000 1px solid; margin-left:200px;"> : : <a href="#" class="alert" rel="shareit" boxtitle="AAA">AAA</a><br/> : : <a href="#" class="alert" rel="shareit" boxtitle="BBB">BBB</a><br/> : : <a href="#" class="alert" rel="shareit" boxtitle="CCC">CCC</a><br/> : : <a href="#" class="alert" rel="shareit" boxtitle="DDD">DDD</a><br/> : : <a href="#" class="alert" rel="shareit" boxtitle="EEE">EEE</a><br/> : : <a href="#" class="alert" rel="shareit" boxtitle="FFF">FFF</a><br/> : : <a href="#" class="alert" rel="shareit" boxtitle="GGG">GGG</a><br/> : : </div> : : <div id="shareit-box" style="display:none;"> : : <div id="shareit-header"></div> : : <div id="shareit-body"> : : <div id="shareit-submitbox"> : : <input name="btn_end" type="button" class="style2" /> : : </div> : : </div> : : </div> : : [jquery] : : $(document).ready(function() : : { : : $('a[rel=shareit]').mousedown(function() : : { : : var height = $(this).height(); : : var top = $(this).offset().top; : : //get the left and find the center value : : var left = $(this).offset().left + : : ($(this).width() /2) - : : ($('#shareit-box').width() / 2); : : $('#shareit-header').height(height); : : $('#shareit-box').show(); : : $('#shareit-box').css({'top':top, 'left':left}); : : var box_title = $(this).attr("boxtitle"); : : $("input[name=btn_end]").click(function() : : { : : alert("結束!"+box_title); // <=測試用的alert : : $('#shareit-box').hide(); : : return false; : : }); : : $('#shareit-header').click(function () { : : $('#shareit-box').hide(); : : }) : : }); : : }); : : 以上,謝謝。 : 你把關閉動作的事件綁在mousedown裡面,所以每次mousedown就會再綁一次關閉動作 : 結局就是你按n次就會有n次關閉動作 : 解決方法是把$("input[name=btn_end]").click(function(){ ... })移出來,另外 : $('#shareit-header').click(function(){ ... })也可以移出來,因為這個動作 : 似乎也沒有必要每mousedown一次就要綁一次 : 2個地方提醒一下 : 1.常用的jquery物件可以設一個變數存起來: : 例如你程式碼裡面常出現的$(this),可以用個變數,比如說var j_obj = $(this);存 : 再用j_obj去操作你要的動作,而且在一個function之中又有function的程式碼裡也可 : 以比較清楚知道現在的this指的是哪一層的this : 2.可以串接就串接 : $('#shareit-box').show(); : $('#shareit-box').css({'top':top, 'left':left}); : 可以寫成 : $('#shareit-box').show().css({'top':top, 'left':left}); : 另外你要設定top和left,別忘了position也要一起設定 : 以下是我改過的程式碼,不過不知道合不合你需求就是了 : $(function(){ : $('a[rel=shareit]').click(function(){ : var j_obj = $(this); : var top = j_obj.offset().top + j_obj.height(); : var left = j_obj.offset().left + (j_obj.width()/2); : $('#shareit-box').show().css({'top':top, 'left':left, 'position':'absolute'}); : $('#shareit-header').text(j_obj.attr("boxtitle")); : }); : $("#shareit-box").click(function(e){ : if(e.target.tagName === "INPUT" && e.target.name === "btn_end"){ : alert("結束!" + $('#shareit-header').text()); : } : $('#shareit-box').hide(); : }); : }); --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 71.104.223.87
1F:推 terrybob:也謝謝這位高手的說明。順便將文章寄回信箱 07/29 13:09







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP