.live()jQuery-如何更改我的事件
如何将这些事件更改为.live()事件处理程序 我想我可以将$(document).ready(function()…更改为…$(document).live(function())以及.keydown和.data更改为.live,但我似乎无法让它工作…请帮助.live()jQuery-如何更改我的事件,jquery,jquery-on,Jquery,Jquery On,如何将这些事件更改为.live()事件处理程序 我想我可以将$(document).ready(function()…更改为…$(document).live(function())以及.keydown和.data更改为.live,但我似乎无法让它工作…请帮助 $(document).ready(function(){ $('#number').bind('keyup change focus', function(){
$(document).ready(function(){
$('#number').bind('keyup change focus', function(){
var field = this;
timer = setTimeout(function(){runSearch(field);}, 444);});
$('#number').keydown(function(){clearTimeout(timer);});
//url selection by specifics
$('#phone_book tr').data('bgcolor', '#aaf').hover(function(){
var $this = $(this);
var newBgc = $(this).data('bgcolor');
$(this).data('bgcolor', $(this).css('background-color')).css('background-color', newBgc);
});
//url selection by specifics
$("#phone_book tr").click(function(){
var id = $(this).children().first().html();
var type = $(this).parents('table').siblings('div.module_header').html().toLowerCase();
改变
到
.live()实际上已被弃用
对于像.keyup(),.keydown()这样的,将它们更改为.on('keydown')等。将.bind()更改为.live()
请记住,较新版本的jQuery不推荐使用live(),您应该改用on()。某些事件可能还需要较旧的delegate()方法
下面是一个使用.on函数的代码示例
$(document).ready(function(){
$('#number').on('keyup change focus', function(){
var field = this;
timer = setTimeout(function(){runSearch(field);}, 444);});
$('#number').keydown(function(){clearTimeout(timer);});
//url selection by specifics
$('#phone_book tr').data('bgcolor', '#aaf').hover(function(){
var newBgc = $(this).data('bgcolor');
$(this).data('bgcolor', $(this).css('background-color')).css('background-color', newBgc);});
//url selection by specifics
$("#phone_book tr").on('click', function(){
var id = $(this).children().first().html();
var type = $(this).parents('table').siblings('div.module_header').html().toLowerCase();
不要更改.ready()。您只需要更改页面初始加载后不存在的元素的处理程序。因此,对于每个动态添加的元素,您必须替换处理程序名称。但是:您应该使用.on(),因为.live()将很快被删除。So.bind、.keydown、.data、.click all需要更改为.on().data()不是事件处理程序,因此不需要更改..data()在jQuery对象上存储或从中检索特定的数据集指定。注意,但这需要的不仅仅是更改。绑定到。在上,您还必须从父级委派。并非始终如此。仅取决于具体情况。如果您的目标元素预计将被删除并重新添加,则是的,委派将是必要的。该问题特别询问如何更改为.live,这建议使用delegated事件。在按下键和显示新结果后,无法使.data()事件起作用。data()不是事件-您是在谈论hover()吗?抱歉,不是数据函数…是的,在没有标记或示例的情况下,我只能猜测。您能设置JSFIDLE吗?
.on(...
$('#number').live('keyup change focus', function(){
var field = this;
timer = setTimeout(function(){runSearch(field);}, 444);});
$('body').delegate('#number', 'keydown', function(){clearTimeout(timer);});
//url selection by specifics
$('body').data('bgcolor', '#aaf').delegate('#phone_book tr', 'hover, function(){
var $this = $(this);
var newBgc = $(this).data('bgcolor');
$(this).data('bgcolor', $(this).css('background-color')).css('background-color', newBgc);
});
$(document).ready(function(){
$('#number').on('keyup change focus', function(){
var field = this;
timer = setTimeout(function(){runSearch(field);}, 444);});
$('#number').keydown(function(){clearTimeout(timer);});
//url selection by specifics
$('#phone_book tr').data('bgcolor', '#aaf').hover(function(){
var newBgc = $(this).data('bgcolor');
$(this).data('bgcolor', $(this).css('background-color')).css('background-color', newBgc);});
//url selection by specifics
$("#phone_book tr").on('click', function(){
var id = $(this).children().first().html();
var type = $(this).parents('table').siblings('div.module_header').html().toLowerCase();