简体   繁体   English

Javascript:识别何时出现新的第二个元素并仅在旧元素上使用 function(突变观察者)

[英]Javascript: Identify when a new, second element appears and use function only on the older one (mutation observer)

I know there are lots of answered questions about the mutation observer on here.我知道这里有很多关于突变观察者的回答问题。 But I couldn't find a solution yet.但我还没有找到解决方案。

On a website, I have a result window that opens when I click on something with a specific tool.在一个网站上,我有一个结果 window 当我使用特定工具单击某些内容时打开。 When I click on another object with that tool, a new result window will open but the old one won't close.当我使用该工具单击另一个 object 时,新结果 window 将打开,但旧结果不会关闭。 So I want that the old window closes when the new one appears .所以我希望旧的 window 在新的出现时关闭 I can do that manually because they have a closing button.我可以手动执行此操作,因为它们有一个关闭按钮。 So I can use a simple click function to close it:所以我可以使用简单的点击 function 来关闭它:

document.querySelector('[title="closeButton"]').click();

The problem is, I just want to close the old window, when a new one appears.问题是,当一个新的出现时,我只想关闭旧的 window。 I tried to use a mutation observer to see when a result window appears, but even that didn't work.我尝试使用突变观察器查看结果 window 何时出现,但即使这样也不起作用。 Although I already used such an observer in another place without problems.虽然我已经在另一个地方使用过这样的观察者没有问题。

function closeResultWindow() {
  let observer = new MutationObserver(function (mutations) {
    let found = mutations.find(mutation => Array.from(mutation.addedNodes)
      .find(node => node.id ?
        node.id.includes("lefttabs_tablist_module_") : false))
    if (found) {
      console.log("Resultwindow found")
      document.querySelector('[title="closeButton"]').click();
      }
  });
  
  observer.observe(document.body, { 
    childList: true, subtree: true 
    });
};

I tried it with different functions with the observer.我与观察者一起尝试了不同的功能。 But this one is a function I already used successfully with another problem and just adjusted it to this one.但是这个是 function 我已经成功地使用了另一个问题,只是将它调整为这个。 The lefttabs_tablist_module_ is the widgetid of the result window. lefttabs_tablist_module_是结果 window 的 widgetid。 Every window has another number behind the lefttabs_tablist_module_ .每个 window 在lefttabs_tablist_module_后面都有另一个数字。

For example window1 has widgetid=lefttabs_tablist_module_1256 and the next window2 has widgetid=lefttabs_tablist_module_7549 .例如 window1 有widgetid=lefttabs_tablist_module_1256 ,下一个widgetid=lefttabs_tablist_module_7549 It doesn't seem to be chronological.它似乎不是按时间顺序排列的。

The observer above was just a test to see if I can identify at least one result window.上面的观察者只是一个测试,看看我是否能识别出至少一个结果 window。 But the goal is to observe when a new window is opened, so there are two, and use the click function to close the older one.但目标是观察一个新的 window 何时打开,所以有两个,并使用单击 function 关闭旧的。

Does anyone have an idea how that would be possible?有谁知道这怎么可能? Or at least what the problem is in my code to identify one window with the observer.或者至少我的代码中有什么问题要与观察者识别一个 window。

Ok guys I solved it.好的,我解决了。 It now works like that:它现在是这样工作的:

function closeResultWindow() {
  let observer = new MutationObserver(function (mutations) {
    let found = document.querySelectorAll('[widgetid^="lefttabs_tablist_module_"]');

    if (found.length >= 2) {
      console.log("Resultwindow found")

      // find old result window
      let closeButton = found[0].querySelector('[title="Close"]');

      // close result window
      if (closeButton) { 
        closeButton.click();
        console.log("old result window closed");
      }
    }
  });
    observer.observe(document.body, { childList: true, subtree: true });
}

closeOerebResultWindow();

Now it instantly closes the old window when I open a new one.现在,当我打开一个新的 window 时,它会立即关闭旧的 window。 Thanks for your help and answers!感谢您的帮助和回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM