jQuery、Ajax和Live函数问题
我知道,如果使用ajax在页面中加载包含表单元素的div,那么必须使用live函数向那些不在dom树中的元素添加事件 而且我在jQuery网站上看到,live功能目前不支持焦点、模糊等 当通过ajax加载到div中的元素被聚焦或模糊时,我应该如何调用函数 是不是应该用bind。。。?但说到绑定,尽管live和bind看起来有点相似,但它不能用于上述场景中。。。对吧 下面是代码jQuery、Ajax和Live函数问题,jquery,ajax,live,Jquery,Ajax,Live,我知道,如果使用ajax在页面中加载包含表单元素的div,那么必须使用live函数向那些不在dom树中的元素添加事件 而且我在jQuery网站上看到,live功能目前不支持焦点、模糊等 当通过ajax加载到div中的元素被聚焦或模糊时,我应该如何调用函数 是不是应该用bind。。。?但说到绑定,尽管live和bind看起来有点相似,但它不能用于上述场景中。。。对吧 下面是代码 <BODY style=""> <div style="margin-top:5px; wi
<BODY style="">
<div style="margin-top:5px; width:100%" class="subAndContent" id="subAndContent">
<!-- TABLE TO HOLD SUB MENU-->
<div id="subMenuDiv">
<table width="100%" >
<tr align="center" valign="middle">
<td width="100%" valign="middle" class="rounded" >
<div class="sidebarmenu">
<ul id="sidebarmenu1">
<li>
<a href="javascript:ajaxLoadMainOnly('createHotel.php', 'content')" > <!-- This function get's the page to be loaded and the div into which it should be loaded and uses ajax to do the loading... -->
HOTEL
</a>
</li>
<li>
<a href="javascript:ajaxLoadMainOnly('createCountry.php', 'content')" >
COUNTRY
</a>
</li>
<li>
<a href="javascript:ajaxLoadMainOnly('createCity.php', 'content')">
CITY
</a>
</li>
</ul>
</div>
</td>
</tr>
</table> <!-- END TABLE TO HOLD SUB MENU-->
</div>
<div id="contentDiv" class="rounded">
<!-- TABLE TO HOLD CONTENT PANE-->
<table width="100%" style="float:left;">
<tr valign="left">
<td align="center">
<div id ="content">
<!-- Div into which the content will be loaded -->
</div>
</td>
</tr>
</table>
</div>
</div>
<!-- DIV AND TABLE TO HOLD FOOTER -->
<?php
include 'footer.php';
?>
</BODY>
我相信我读到了jQuery的下一个版本(1.4)包含了Live的剩余事件
但是现在,在1.3中,您需要使用bind(或者像“click”这样的快捷方式)。是的,如果你在页面上添加了元素,而Live对你正在做的事情不起作用,你需要绑定添加的内容
这是一个很好的开始。如果你想处理Live尚未处理的事件,我认为你仍然可以使用liveQuery插件。这里有一个简单的例子: 这将在每次调用ajax请求时取消绑定并重新绑定焦点事件。解除绑定只是为了安全,并确保没有任何遗留事件
$(document).ready(function() {
focusEventFunction();
ajaxLoader();
}
function ajaxLoader();
$('#sidebarmenu1 a').unbind().bind('click', function(event){
$.get('url_to_get.php', function(data) {
// CODE THAT REPLACES DIVS AND DATA
//The following has to be inside the ajax callback and
//after the code that changes divs. this will remap all the functions
//that bind the focus.
$('selector_for_elements_needing_focus').unbind().bind('focus', function(event){
focusEventFunction($(this), event)
ajaxLoader(); //this ensures your ajax gets called again,
//depending on the complexity of the html changes
//you may not need this.
}
}
function focusEventFunction(jElement, event) {
//just a dummy function that does your focus/blur stuff.
// you might not need the parameters
}
您必须掌握动态加载的元素,然后使用bind添加焦点和模糊处理程序。例如,如果要将处理程序添加到类为“longtext”的textarea中,可以使用以下方法:
$.getJSON('createHotel.php', { /* params go here */ }, receiveHotelCreated);
function receiveHotelCreated(data) {
$('#content textarea.longtext').bind('blur', onTABlur);
}
function onTABlur() {
var ta = $(this); // The textarea on which the blur event occurred
alert('Textarea blurred');
// Your code goes here
}
当焦点离开文本区域时,将调用onTABlur(TA=文本区域)函数。在函数内部,
此
指调用函数的元素。当我们收到AJAX响应(receiveHotelCreated
)时将其绑定到文本区域,这将是所需的文本区域元素。我们在jQuery函数($(this)
)中包装这个
,以获得一些额外的功能。你从手机发短信了吗?:-)不,我没有。。。。有什么不对劲吗?“u”和“r”加上没有大写字母,读起来有点困难。:-)哎呀。。。!!我的错。。。!!从现在开始会更清楚。。。谢谢你的指点……:)你能举几个例子吗。。。?我的印象是bind不会做我想做的事情……我认为bind不会做,因为我不认为它会对单独加载了ajax的元素起作用……您是否有任何代码,以便我可以看到哪些不适合您?我不确定你是如何加载新内容的。我想在focus事件中调用函数。查看livequery,它是否支持focus和blur直播…?谢谢你的建议。。。我会尝试这两种方法,然后很快给你回复……嗨,汤姆,你能详细介绍一下onTABlur函数吗,特别是$(e.target)…?记得你答应过写“你”而不是“你”;-)我删除了$(e.target)
,因为您在这里并不真正需要它,我想这有点让人困惑。关于onTABlur()
,有什么具体不清楚的地方?