简体   繁体   English

如何在 Javascript 中为 onclick PopUp 添加事件监听器?

[英]How to add event Listener for onclick PopUp In Javascript?

Consider there is some hyperlink tag that makes the API call to backend.考虑有一些超链接标签使 API 调用后端。

Example:例子:

<a  href="/get/contact-info/" id="ember107" >Contact info </a>

After the backend API call completed it will trigger/open a popup in that page.后端 API 调用完成后,它将触发/打开该页面中的弹出窗口。

Popup data: (Sample one div data)弹出数据:(示例一个 div 数据)

<div id="ember"> <h1 id="pv-contact-info"> Contact Name</h1></div>

My objective is to extract data from this popup (above tag).我的目标是从此弹出窗口(标签上方)中提取数据。 Lets say Contact Name from h1 tag.让我们说来自 h1 标签的联系人姓名。

what I tried so far:到目前为止我尝试了什么:

let atag = document.getElementById("ember107");
atag.addEventListener('click', () => { 
    document.getElementById("pv-contact-info").innerText; // getting from popup h1 tag
});
atag.click(); // explicit click

The Problem I faced is Uncaught TypeError: Cannot read property 'click' of null when this statement is executed document.getElementById("pv-contact-info").innerText;我面临的问题Uncaught TypeError: Cannot read property 'click' of null when this statement is executed document.getElementById("pv-contact-info").innerText;

I know the problem is popup content was not loaded completely that's why this code document.getElementById("pv-contact-info") returning null.我知道问题是弹出内容没有完全加载,这就是为什么这个代码document.getElementById("pv-contact-info")返回 null。

My Question is whether there is any listener function to check Popup content is loaded completely or we can do this in another approach.我的问题是是否有任何侦听器 function 来检查弹出内容是否已完全加载,或者我们可以用另一种方法来做到这一点。 Most preferable using browser support /vanilla javascript rather than library.最好使用浏览器支持 /vanilla javascript 而不是库。

You can use a MutationObserver to watch for changes to the DOM on the page.您可以使用 MutationObserver 来监视页面上 DOM 的更改。 MDN . MDN

If you need to stay compatible with older browsers use your click event to trigger your own manual watcher.如果您需要与旧版浏览器保持兼容,请使用您的点击事件来触发您自己的手动观察器。 Something like:就像是:

var interval_id = false;
function lookForPopup(){
    if(interval_id){
        clearInterval(interval_id);
    }else{
        interval_id = setInterval(function(){
            var popup = document.getElementById('some-id');
            if(popup){
                // do something

                clearInterval(interval_id);
            }
        },1000);
    }
}

As @Gavin already suggested to use MutationObserver I like to elaborate a little bit.正如@Gavin 已经建议使用MutationObserver我想详细说明一下。

How I fixed this issue using MutationObserver .我如何使用MutationObserver解决此问题。

MutationObserver has an ability to watch for changes being made to the DOM tree.
 I mention the code below how it fixed my issue/requirement.


 // selecting the href by id and triggering a click event.
 let hrefDoc = document.getElementById("ember");
 divDoc.click();


let m = new MutationObserver(() => {
  let divDoc = document.getElementById("ember");
    new MutationObserver( () => {
          // Finally Extracting the required data
          console.log(document.getElementById("pv-contact-info").innerText);
        }
      }).observe(divDoc || document,observerOptions);
  });
m.observe(hrefDoc,observerOptions);

As you can see above I added another child MutationObserver to observe the divDoc element.正如你在上面看到的,我添加了另一个子 MutationObserver 来观察 divDoc 元素。 That is because it is when the hrefDoc click event the parent MutationObserver callback is called with some observeroptions.那是因为当hrefDoc点击事件时,父MutationObserver回调被一些观察者选项调用。 But in that case I could not get my <div id="ember"> Mutation Node.但在那种情况下,我无法获得我的<div id="ember">变异节点。

That's the reason I added another this statement let divDoc = document.getElementById("ember");这就是我添加另一个此语句的原因let divDoc = document.getElementById("ember"); and waiting for this targetNode in the child MutationObserver .并在子MutationObserver中等待这个 targetNode 。 Finally I extracted the data from this tag <h1 id="pv-contact-info">最后我从这个标签<h1 id="pv-contact-info">中提取了数据

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

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