![](/img/trans.png)
[英]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.