Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 监视列表中的第一个div_Javascript - Fatal编程技术网

Javascript 监视列表中的第一个div

Javascript 监视列表中的第一个div,javascript,Javascript,我想做一个油腻的剧本。他必须观看偶尔出现优惠券的页面,并将其拿走 优惠券上有一张 我希望跟踪第一个元素,并且为了这个用途,使用MutationObserver。 问题是站点使用ajax,第一张优惠券变成了第二张优惠券,以此类推。MutationObserver继续监视它,并没有注意到新的优惠券。请帮助,我如何才能只查看列表元素div中的顶部(第一个) // select the target node var target = document.querySelector('.id'); //

我想做一个油腻的剧本。他必须观看偶尔出现优惠券的页面,并将其拿走

优惠券上有一张 我希望跟踪第一个元素,并且为了这个用途,使用MutationObserver。 问题是站点使用ajax,第一张优惠券变成了第二张优惠券,以此类推。MutationObserver继续监视它,并没有注意到新的优惠券。请帮助,我如何才能只查看列表元素div中的顶部(第一个)

// select the target node
var target = document.querySelector('.id');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.type);
  });    
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);
更新:

// select the target node
var target = document.querySelector('.id');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.addedNodes[0].textContent);
    document.getElementsByClassName('nameshop')[0].click();
  });    
});

它提供了一个带有div class=“id”的新元素,并单击我想要的位置。但如果我通过Firebug进行更改,就会发生这种情况。如果站点被更新,脚本不会注意到。

您也可以这样使用突变结果:它提供addedNodes属性。因此,如果添加了一个节点,只需使用translations.addedNodes(NodeList类型)。有关此信息,请参见:

var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.addedNodes[0]);
  });    
});