addEventListener加载ajax加载没有jquery [英] addEventListener load on ajax load WITHOUT jquery

查看:296
本文介绍了addEventListener加载ajax加载没有jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个html文件和4个javascript文件。每个javascript文件都是从每个html文件外部加载的。

I have 4 html files and 4 javascript files. Each javascript file is loaded externally from each html file

index.html加载javascript.js
1.html加载javascript1.js
2.html加载javascript2.js
3.html加载javascript3.js

index.html loads javascript.js 1.html loads javascript1.js 2.html loads javascript2.js 3.html loads javascript3.js

我遇到的问题是index.html使用AJAX加载其中一个按下特定按钮时的其他页面。然后应该发生的是特定页面将加载其自己的相应javascript程序,该程序在该程序的末尾包含应该在load上运行的window.addEventListener并运行函数registerListeners。 RegisterListeners然后在该页面上的按钮上注册一个事件监听器,currentyl只显示一个测试警报。

The issue that I'm having is that index.html uses AJAX to load one of the 3 other pages when a specific button is pressed. Then what is supposed to happen is that particular page would load its own corresponding javascript program which at the end of that program contains a window.addEventListener that should run on "load" and run the function "registerListeners". RegisterListeners then registers an event listener on a button on that page which currentyl just displays an alert for testing.

我想到的是窗口的参数load当使用ajax动态加载页面时,.addEventLisener似乎无效。

What I have figured out is that the parameter "load" for window.addEventLisener doesnt seem to be valid when the page is loaded dynamically with ajax.

我无法将所有代码放在主javascript中,因为它包含一些调用的getElementById调用来自其中一个编号页面的元素,如果在主页面加载时执行会产生错误,因为那些特定的ID还不存在。

I cant put all of the code in the main javascript because it contains some getElementById calls that call elements from one of the numbered pages that, if executed on main page load will generate errors because those specific ID's dont exist yet.

这是功课,所以我需要得到使用html5和javascript withOUT jquery指向正确的方向。我明白用jquery可能会更容易,但我需要弄清楚如何在没有暂时的情况下做到这一点。

This is homework and so I need to get pointed in the right direction to doing this with html5 and javascript WITHOUT jquery. I understand that it might be easier with jquery, but I need to figure out how to do it without for the time being.

会不会有窗口这样的东西。 addEventListener(AJAXload,someFunction(),false)???

Would there be such a thing as window.addEventListener("AJAXload", someFunction(), false) ???

推荐答案

不是以编程方式添加事件监听器,而是可能会在AJAX内容的末尾插入一个简单的Javascript函数。这将在加载内容时运行,在父窗口中调用内容。

Instead of adding an event listener programatically, you could possibly insert a bare Javascript function at the end of your AJAX'ed content. This would be run when the content is loaded, calling something in the parent window.

AJAX BLAH BLAH BLAH
<script type="text/javascript">
  childContentLoaded();  //or whatever you need 
</script>

您可能还需要添加parent.functionName()

you may also need to add "parent".functionName()

这篇关于addEventListener加载ajax加载没有jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆