jQuery更新后,弹出窗口停止工作

我一直在使用这个脚本一段时间(最初是由Soh Tanaka编写的,但源网站已经不见了) – 它在一个黑暗的页面上弹出一个窗口,其中有一个关闭按钮,可以解除它并使页面不受影响。 它工作正常,直到我将jquery更新到最新的1.9.1以实现一些新的东西。 现在它弹出窗口但单击关闭按钮不再将其删除 – 它只是将背景中的页面分流到顶部,它似乎为背景添加了另一层黑暗。

错误控制台消息: TypeError:’undefined’不是一个函数(评估’$(’a.close,#fade’)。live’)引用脚本的最后一段://关闭弹出窗口……“

谁能帮我解决问题呢? 相当漂亮的菜鸟,更多的剪切和粘贴的人! Thanx 🙂

 $("document").ready(function() { $('a.poplight[href^=#]').click(function() { var popID = $(this).attr('rel'); //Get Popup Name var popURL = $(this).attr('href'); //Get Popup href to define size //Pull Query & Variables from href URL var query= popURL.split('?'); var dim= query[1].split('&'); var popWidth = dim[0].split('=')[1]; //Gets the first query string value //Fade in the Popup and add close button $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend(''); //Define margin for center alignment (vertical horizontal) - we add 80px to the height/width to accomodate for the padding and border width defined in the css var popMargTop = ($('#' + popID).height() + 80) / 2; var popMargLeft = ($('#' + popID).width() + 80) / 2; //Apply Margin to Popup $('#' + popID).css({ 'margin-top' : -popMargTop, 'margin-left' : -popMargLeft }); //Fade in Background $('body').append('
'); //Add the fade layer to bottom of the body tag. $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies return false; }); //Close Popups and Fade Layer $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer... $('#fade , .popup_block').fadeOut(function() { $('#fade, a.close').remove(); //fade them both out }); return false; }); });

您可以添加迁移插件来解决此问题。

在jQuery 1.9中 ,删除了许多已弃用的方法。 jQuery.live是已删除的方法之一,您可以使用jQuery.on作为live的替代品。

但是如果你有其他dependend库使用这些不推荐使用的function,那么你可以使用jQuery 迁移插件来实现向后兼容。 它将几乎所有删除的function添加回jQuery。

在您的代码中, live()事件注册可以更改如下

 $(document).on('click', 'a.close, #fade', function() { //When clicking on the close or fade layer... $('#fade , .popup_block').fadeOut(function() { $('#fade, a.close').remove(); //fade them both out }); return false; }); 

.live已从1.9中删除。 您可以替换此语法:

 $('selector').live('event', function(e) { 

附:

 $(document).on('event', 'selector', function(e) {