如何为来自服务器的元素添加事件监听器?

发布于 2024-08-04 17:06:02 字数 93 浏览 1 评论 0原文

如何使用addEventListener(window.onload)添加点击事件 当相关标签生成时 服务器(通过 xmphttp 请求,不少于)?

谢谢!

How do I add a click event using addEventListener (window.onload)
when the tags in question are being generated from
the server (via an xmphttp request, no less)?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失与倦" 2024-08-11 17:06:02

您可以尝试处理父元素的事件,这些事件在 DOM 加载时可用,然后获取与事件相关的元素。

<ul id="list">
  <li id="first">The first</li>
  <li id="second">The second</li>
  <li id="third">The third</li>
</ul>

document.getElementById('list').onclick(function(e){
  o = e.originalTarget;
  // if you click on second li o will bi the same as document.getElementById('first')
  // even if li with id "first" is inserted to DOM after creating this event handler to "list"
  // so here you can perform actions with it
  // hope it will help
});

You can try to handle events for parent elements, which are available as DOM loaded, then get element related to event.

<ul id="list">
  <li id="first">The first</li>
  <li id="second">The second</li>
  <li id="third">The third</li>
</ul>

document.getElementById('list').onclick(function(e){
  o = e.originalTarget;
  // if you click on second li o will bi the same as document.getElementById('first')
  // even if li with id "first" is inserted to DOM after creating this event handler to "list"
  // so here you can perform actions with it
  // hope it will help
});
吃素的狼 2024-08-11 17:06:02

谢谢大家。

我通过将以下代码添加到 XMLHTTP 请求的“成功时”事件解决了这个问题,该请求使用来自服务器的元素填充 DOM。这对我有用。
乔希,你让我的头脑朝着正确的方向发展(尽管很高兴看到代码插图),所以我将你的回复标记为答案。

if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {

                        var m_sel=document.getElementById("fcat");

                        if (m_sel) {

                            var maxi = m_sel.options.length;

                            for( var i = 0; i < maxi; i++ )
                            {
                                var option = m_sel.options[i];
                                    option.addEventListener( "click", toggleElem, true );

                            }                                 

                        }        
}

Thanks all.

I solved this by adding the below code to the "on success" event of the XMLHTTP request that populated the DOM with the elements coming from the server. That worked for me.
Josh, you got my head moving in the right direction (although it would have been nice to see a code illustration) so I marked your response as the answer.

if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {

                        var m_sel=document.getElementById("fcat");

                        if (m_sel) {

                            var maxi = m_sel.options.length;

                            for( var i = 0; i < maxi; i++ )
                            {
                                var option = m_sel.options[i];
                                    option.addEventListener( "click", toggleElem, true );

                            }                                 

                        }        
}
微凉徒眸意 2024-08-11 17:06:02

您必须在元素插入 DOM 后应用事件处理程序

You have to apply the event hanlders after the elements have been inserted into the DOM

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文