[英]Change dynamically created element
I'm building browser plugin (safari extension) that automatically changes an element (by "prepend"). 我正在构建自动更改元素(通过“ prepend”)的浏览器插件(safari扩展)。 It can be done by calling prepend like: 可以通过调用prepend来完成,例如:
$(document).ready(function(){
$("a").prepend("<img ...>").
})
But, how can I change elements that are created dynamically using javascript? 但是,如何更改使用javascript动态创建的元素?
You can 1st create the object: 您可以1创建对象:
$(document).ready(function(){
var img = $("<img ...>");
$("a").prepend(img);
// do something with img
});
You could listen for DOM change: 您可以听DOM更改:
$("body").on("DOMSubtreeModified", function(){
alert('DOM changed');
});
And than figure out what has changed. 而不是找出发生了什么变化。 But that isn't clear from your question what you would like to do. 但这从您的问题中不清楚您想做什么。
You can store the object you create in a global variable so it is available throughout your webpage... Like so: 您可以将创建的对象存储在全局变量中,以便在整个网页中都可以使用...像这样:
var img;
$(document).ready(function(){
img = $("<img ...>");
$("a").prepend(img).
});
Then later on in some other function or something... 然后再执行其他一些功能...
$(img).click(function() {
// blah
});
You can create trigger, 您可以创建触发器,
$(document).ready(function(){
$("a").prepend("<img id="imgId"...>").
$(document).trigger('asd');
})
$(document).on('asd', function() {
});
Or you can create event listner 或者您可以创建事件列表器
$(document).ready(function(){
$("a").prepend("<img id="imgId"...>").
})
$(document).on('click','imgId',function(){
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.