求求你,我真的不是程序员。
当一个子元素被附加到一个div后,我需要截取。
就像下面这个例子(没有超时):
$(document).ready(function(){
setTimeout(function(){
$('#holder').append('<div id="device">Test</div>');}, 2000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="holder">
<!––child dynamicly inserted-->
</div>
</body>
我使用了变分观察器,但是接缝似乎被弃用了……我看到了proxy(),但我不知道如何使用它...
我的变异观察者代码:
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations, observer) {
mutations.forEach(function(mutation) {
if ($('#main-view div.row').next().length != 0 ){
DelRow();
};
$('#main-view div.row.divider .span4').toggleClass('span4 tile');
});
});
$( document ).ready(function() {
if (!isMobile){
observer.observe(targetedNode, {
childList: true,
subtree: true
});
但是不要在手机上工作..
发布于 2018-10-26 15:19:57
突变观察者工作得很好,并且没有被弃用。我一直在用它。这是一个侦听事件,然后警告是否添加或减去div的示例。尽量将本演示中的技术应用到您自己的用例中。
<div id="holder">
<h2>Let's Add Div's Dynamically</h2>
</div>
<button id="button" type="button">Add New Div</button>
<script>
const btn = $('#button');
const holder = $('#holder');
// append a new div to out holder element
function addDiv(){
holder.append('<div>A Great New Div!! :D</div>');
}
// attach click function to btn
btn.click(addDiv);
// Create an observer instance linked to the callback function
var observer = new MutationObserver(function(mutationsList, observer) {
$(mutationsList).each(function(index, item){
if (item.type === 'childList'){
if (item.addedNodes.length > 0){
alert('new div is being added!');
}
if (item.removedNodes.length > 0){
alert('div has been removed');
}
}
});
});
// Start observing the target node for configured mutations
observer.observe(holder[0], {
attributes: true,
childList: true,
subtree: true
});
</script>
https://stackoverflow.com/questions/52983284
复制相似问题