更新时间:2023-11-24 12:07:52
如果可能,我会尝试从其他脚本中调用所需的函数.但是,如果您真的想听元素上的更改,则可以使用DOMSubtreeModified
.如果您通过jQuery更改内容,尽管它将调用您的函数两次!因此,请使用普通JavaScript更改内容.
If possible I would try to call the wished function from the other script. But if you really want to listen for changes on the element you can make use of DOMSubtreeModified
. If you change the content via jQuery though it will call your function twice! Therefore use vanilla JavaScript for changing the content.
function versandkosten() {
console.log('called versandkosten');
}
$(document).ready(function(){
var $basketAmount = $('.basket_amount');
$basketAmount.bind("DOMSubtreeModified", versandkosten);
// test changing the content of the element via JavaScript
$basketAmount[0].innerHTML = 77;
// changing it via jQuery would call it twice!
// $basketAmount.html(77);
});
提琴: https://jsfiddle.net/9our3m8a/
尽管这仍然应该起作用,但是请注意,DOMSubtreeModified
已被弃用,另请参见
While this should still work, be aware that DOMSubtreeModified
has been deprecated see also Mozilla Doc. Thx to @LuisEduardox for pointing this out.