1 回答

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
});
顯然沒有在該插件中實現。
添加回答
舉報