java弹窗 触发事件,如何在Java中为onclick弹出窗口添加事件监听器?

这篇博客讨论了在JavaScript中如何处理后台API调用后打开的弹窗数据提取问题。作者遇到的问题是尝试获取弹窗内h1标签的ContactName时,由于弹窗内容未完全加载导致错误。解决方案是利用MutationObserver来观察DOM变化,当弹窗内容加载完成后执行相应操作。这种方法可以在不完全依赖特定时间间隔的情况下,确保数据提取的准确性。
摘要由CSDN通过智能技术生成

Consider there is some hyperlink tag that makes the API call to backend.

Example:

Contact info

After the backend API call completed it will trigger/open a popup in that page.

Popup data: (Sample one div data)

Contact Name

My objective is to extract data from this popup (above tag). Lets say Contact Name from h1 tag.

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;

I know the problem is popup content was not loaded completely that's why this code document.getElementById("pv-contact-info") returning 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. Most preferable using browser support /vanilla javascript rather than library.

解决方案

You can use a MutationObserver to watch for changes to the DOM on the page. 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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值