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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

文本區域的內聯編輯 – 如何獲取 ID?

文本區域的內聯編輯 – 如何獲取 ID?

三國紛爭 2022-09-23 16:24:12
我使用jqInline編輯在網頁上進行內聯編輯。一切都有效,除了我不知道如何獲取將更改保存到數據庫(通過Django)所需的項目。id上下文如下所示:<div id="remark14756" class="remark" data-cid="14756">    Sample Text</div>這是腳本:<script src="/static/inline-edit.jquery.js"></script><script>    $(".remark").inlineEdit({        type: 'textarea',        onChange: function (e, text, html) {            // Executes when exiting inline edit mode and a change has been made            c_id = $(this).attr("data-cid");            alert("Test: ", c_id)        }    });</script>顯然,在這種情況下不起作用。我嘗試了一切,搜索了很多,但我找不到如何以正確的方式做到這一點。有人知道答案嗎?$(this)
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

內聯編輯文檔說:


on更改(此,文本,html) - 在退出內聯編輯模式并已進行更改時執行


使用相當具有誤導性。this


因此,第一個參數實際上是元素。


$(".remark").inlineEdit({

    type: 'textarea',


    onChange: function (elem, text, html) {


       // `this` refers to inlineEdit instance plugin

       // `elem` is the currently edited element


       const c_id = $(elem).attr("data-cid");

       alert(c_id);  // 14756

    }

});

該插件未以預期的“jQuery插件”方式執行。

通常正確編寫的插件應該:


將所有方法綁定到元素被調用方,

(對于 Event 方法),第一個參數應始終引用原始事件。

允許開發人員使用關鍵字引用它以獲取本機JS元素,或者在公開的公共方法中執行操作,就像我們對本機jQuery方法的期望一樣,并且可以訪問事件(即:如果我們使用箭頭函數來提取關鍵字不存在,則很有用)this$(this)currentTargetthis


$someElem.on('click', function(evt) {

  const $el = $(this); // what we're used to

});


$someElem.on('click', (evt) => {

  const $el = $(evt.currentTarget); // the importance of always passing the Event as first param

});

顯然沒有在該插件中實現。


查看完整回答
反對 回復 2022-09-23
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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