te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - MutationObserver not working for children - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MutationObserver not working for children - Stack Overflow

programmeradmin3浏览0评论

Apologies in advance for what is probably a simple question and appalling javascript below;

My problem is as follows, there is a banner on a site that goes through four images every few seconds. I am trying to push an "impression" into the dataLayer to be picked up by GTM. To display the next image it has been made (not by myself) to change the z-index from 0 to 1 of the next banner image. I tried initially to get the mutation observer to work on just one image. This worked but I soon found out that the z-index value actually changes about 3 times before settling on 1, so that actually fired 3 impressions each time. Ideally however I would like it all to work by looking at the parent div (and observing childList) and only fire one impression per banner image, but when I try that it just says 'the provided node was null', I wonder if it is to do with the fact that only the children's style attributes are changing and nothing else?

The banner HTML is (when imagePane2 is shown);

<div id="rotator_10690" class="imageRotator Homepage_Middle_Banner_Rotator">
<div class="imagePane Homepage_Middle_Banner_imagePane imagePane1" style="left: 0px; top: 0px; position: absolute; z-index: 0; opacity: 1; display: none;">
<div class="imagePane Homepage_Middle_Banner_imagePane imagePane2" style="left: 0px; top: 0px; position: absolute; z-index: 1;">
<div class="imagePane Homepage_Middle_Banner_imagePane imagePane3" style="left: 0px; top: 0px; position: absolute; z-index: 0; display: none;">
<div class="imagePane Homepage_Middle_Banner_imagePane imagePane4" style="left: 0px; top: 0px; position: absolute; z-index: 0; display: none;">

Apologies in advance for what is probably a simple question and appalling javascript below;

My problem is as follows, there is a banner on a site that goes through four images every few seconds. I am trying to push an "impression" into the dataLayer to be picked up by GTM. To display the next image it has been made (not by myself) to change the z-index from 0 to 1 of the next banner image. I tried initially to get the mutation observer to work on just one image. This worked but I soon found out that the z-index value actually changes about 3 times before settling on 1, so that actually fired 3 impressions each time. Ideally however I would like it all to work by looking at the parent div (and observing childList) and only fire one impression per banner image, but when I try that it just says 'the provided node was null', I wonder if it is to do with the fact that only the children's style attributes are changing and nothing else?

The banner HTML is (when imagePane2 is shown);

<div id="rotator_10690" class="imageRotator Homepage_Middle_Banner_Rotator">
<div class="imagePane Homepage_Middle_Banner_imagePane imagePane1" style="left: 0px; top: 0px; position: absolute; z-index: 0; opacity: 1; display: none;">
<div class="imagePane Homepage_Middle_Banner_imagePane imagePane2" style="left: 0px; top: 0px; position: absolute; z-index: 1;">
<div class="imagePane Homepage_Middle_Banner_imagePane imagePane3" style="left: 0px; top: 0px; position: absolute; z-index: 0; display: none;">
<div class="imagePane Homepage_Middle_Banner_imagePane imagePane4" style="left: 0px; top: 0px; position: absolute; z-index: 0; display: none;">

My script for the parent div is

<script>
// select the target node
var target = document.querySelector('.rotator_10690');

//call mutation observer api
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
 
// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
      if(mutation.type==="attributes" && mutation.target.className.indexOf("imagePane")) {
         observer.disconnect();
         dataLayer.push({'event': 'paneImpression'});

      }
  });
});
 
var disconnect = observer.disconnect();
// configuration of the observer:
// pass in the target node, as well as the observer options
var config = { attributes: true, childList: true }
observer.observe(target, config);

And for just the imagePane2 (I tried mutations.some here with return false, to try to stop it after it received the first mutation, but this didn't work. I also had zIndex==="1" in here as well but that still meant 3 or more impressions firing each time.).

<script>
// select the target node
var target = document.querySelector('.imagePane2');

//call mutation observer api
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
 
// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.some(function(mutation) {
      if(mutation.type==="attributes" && mutation.target.className.indexOf("imagePane2")) {
         observer.disconnect();
         dataLayer.push({'event': 'paneImpression', 'pane': 'two'});

         return false;

      }
  });
});
 
var disconnect = observer.disconnect();
// configuration of the observer:
// pass in the target node, as well as the observer options
var config = { attributes: true }
observer.observe(target, config);


 

</script>

Any help anyone could offer would be greatly appreciated, I have tried looking everywhere but couldn't get anything to work.

Thank you

Share Improve this question asked Jul 3, 2015 at 7:25 MattMatt 1031 gold badge2 silver badges5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 15
{ attributes: true, childList: true }

asks for mutations due to changes to the attributes of .rotator_10690 and for mutations due to changes to the list of elements that are children of the code. childList literally listens for changes to the list of children, it doesn't listen for changes to the children themselves.

If you want essentially get all mutations to all children, the same way DOM events propagate up the tree, you'd want to add

subtree: true  

You need to add subtree: true to the config.

发布评论

评论列表(0)

  1. 暂无评论