jQuery的attr()屬性__jquery

來源:互聯網
上載者:User
問題描述

在咕咕雲的開發中,需要用到ajax的方法顯示任務詳情,在此頁面上回複任務,刪除評論,評論評論(即評論的蓋樓),但是蓋樓的功能一直想不到怎麼解決。 JavaScript代碼解決

var cid = data.data.Task.comment[i].cid;replyComment.id = cidreplyComment.addEventListener("click",function(){document.getElementsByName("task_id")[0].value = id;document.getElementById("ParentComment").value = replyComment.id;alert(replyComment.id);$("#comment_area").focus();//聚焦到textarea用以輸入評論內容},false);
cid是利用ajax方法從後台傳到前端的本條評論的Id 把評論的Id值賦給replyComment,即每一個回複按鈕綁定到對應的評論 給每個回複按鈕綁定監聽時間,當點擊回複時觸發匿名函數,其中addEventListener()函數的第三個參數代表了事件機製為冒泡還是捕獲,參考JS的事件機制詳解 利用上述函數,啟動並執行結果是出錯。由於上述代碼是在運行在for中,因此每次alert的值都是迴圈的最後一個評論此Id,並沒有起到綁定的作用,這就是坑所在的地方。
var cid = data.data.Task.comment[i].cid;replyComment.id = cidreplyComment.addEventListener("click",function(){var strTemp = $(this).attr("id");document.getElementsByName("task_id")[0].value = id;document.getElementById("ParentComment").value = $(this).attr("id");$("#comment_area").focus();//聚焦到textarea用以輸入評論內容},false);
修改為上訴代碼,利用$(this).attr("id")實現擷取回複按鈕綁定的評論,把它的Id作為父節點Id傳到後台,則新添加的評論為其子評論,實現了蓋樓的功能。
詳解js和jquery裡的this關鍵字
jQuery的attr()屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.