jQuery的。对功能似乎并不喜欢'负荷'事件并不、负荷、事件、功能

2023-09-10 17:30:04 作者:弃疗之后精神倍棒

我有一个完全的AJAX驱动的网站,所以最近我已经委托研究技术。我已经学会了.live和.delegate已经去precated和新的功能,采用的是。对。

I have a completely ajax driven website, so lately I've had to research delegation techniques. I've learned that .live and .delegate have been deprecated and the new function to use is .on .

如果我是动态地加载网页为司当前页面(AJAX)上,像这样:

If I'm dynamically loading a webpage into a division on the current page (AJAX) like so:

<html>
<head>
<script src="jquery.js">
</script>
<script>
function ajaxTest()
{
    $('#content').load('test.php');
}
</script>
<script type="text/javascript">
$(function(){
    $(document).on("click", "#map", function(){
        alert("it has been loaded");
    });
});
</script>
</head>
<body>
    <div id="content">
        <button onClick="ajaxTest()" value="Click Me">
            This is to be clicked
        </button>
    </div>
</body>
</html>

在这里test.php的样子

where test.php looks like

<html>
  <head>
  </head>
  <body>
    <div id="map">THIS IS THE MAP</div>
  </body>
</html>

我可以再点击的话这是图,它确实显示了警报。我一直有这个问题是不是这样做的:

I can then click on the words "THIS IS THE MAP" and it does indeed show the alert. The problem I've been having is instead of doing:

$(document).on("**click**", "#map", function(){

我需要的线沿线的更多的东西:

I need something more along the lines of:

$(document).on("**load**", "#map", function(){

这不明明工作,如果类似的东西的威力我想知道。整个原因,我甚询问这是因为在某些网页,而不必只是这是地图在地图上师,我有一个谷歌地图或SWF对象或东西。任何帮助将是AP preciated。

It doesn't work obviously, so I'm wondering if something similar might. The whole reason I'm even inquiring about this is because in some pages, instead of having just "THIS IS THE MAP" in the map division, I have a google map or an swf object or something. Any help would be appreciated.

如果你想只是回答如何加载谷歌地图变成一个部门,还不存在,这将是也有帮助;)

If you wanted to just answer how to load a google map into a division that doesn't exist yet, that would be helpful too ;)

推荐答案

只有一些事件冒泡父链,可用于父对象与。对() .live() .delegate() .load()是不是泡沫的事件之一。

Only some events bubble up the parent chain and can be used on parent objects with .on() or .live() or .delegate(). .load() is not one of those events that bubbles.

这是事件的部分列表从jQuery DOC泡沫:单击,双击自动,的keydown,按键preSS,KEYUP,鼠标按下,鼠标移动,mouseout事件,鼠标悬停和鼠标松开

This is a partial list of events that bubble from the jQuery doc: click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup.

这是对。对()的文档页面 < /一>,这里的报价:

From the doc page for .on(), here's a quote:

在所有的浏览器,加载该事件不会冒泡。

In all browsers, the load event does not bubble.

在你的特殊的例子,您可以将事件直接绑定到这样的对象:

In your particular example, you can bind the event directly to the object like this:

$("#map").on("load", function(){});

$("#map").load(function(){});

当你直接绑定到这样的对象,没有起泡是必要的,它将与负载事件。对象 #map 将需要你绑定事件处理程序使用此方法时存在。对() .load()不能使用将在未来创建的对象的工作。事件处理程序必须连接到创建对象之后的对象。

When you bind directly to the object like this, no bubbling is needed and it will work with the load event. The object #map will need to exist at the time you bind the event handler as this method of using .on() or .load() can't work with objects that will be created in the future. The event handler must be attached to the object after the object is created.