[英]How to call a function in every element in the DOM even if they are dynamically created
[英]How to call a jQuery function on dynamically created element
基於ajax調用,我正在頁面上創建動態元素(按鈕),我確實需要調用另一個ajax函數來設置其屬性。
我想在創建時設置其數據屬性。
<button class="light UserInfo_" data-toggle="dropdown" aria-expanded="true"> data.DriverName </button>
我已經嘗試過以下功能,但無法正常工作。
jQuery(document).on('DOMNodeInserted', '.UserInfo_', function () {
jQuery(this).encryptIt();
});
function encryptIt() {
alert('hi');
};
嘗試如下。
responseJSON:{element: "<input id='n1'>", id: "n1", attr: "value", value="Hi"}
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
}
});
return result;
}
var result = foo().done(function(){
console.log(result.responseJSON)
$("#mainCont").append(result.responseJSON["element"]);
$("#"+result.responseJSON["id"]).attr(result.responseJSON["attr"],result.responseJSON["value"])
});
的HTML
<div id="mainCont"></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.