$ Object不支持属性或方法'live'

时间:2014-01-03 18:04:43

标签: javascript jquery internet-explorer

我在这里有一些脚本在IE中引发错误,因为实时方法。测试网站网址为@ http://8020demo.com/ReadyRange

我已经阅读了许多修复程序,并尝试了各种方法来修复它,但到目前为止没有运气 - 任何帮助都将受到赞赏。感谢。

  • 我使用的是最新的Jquery版本1.10.2
  • 我也在使用Jquery migrate 1.2.1

当前代码:

function slider(selector,xml)
{

// pre-loads slider images into cache (*used for a smooth slide transition on initial page load)
slide1 = new Image(960,455);
slide1.src = "./img/carouselSlide1.jpg";

slide2 = new Image(960,455);
slide2.src = "./img/carouselSlide2.jpg";

slide3 = new Image(960,455);
slide3.src = "./img/carouselSlide3.jpg";

function Slide2Next(selector,count,content,items,slideID)
{   
    if(slideID>0) count = slideID;
    else if(count<items) count++;
    else count = 1; // reached the last slide, get back to first

    $(selector+' .splash_content').animate({opacity:'toggle'},250, function(){
    // faded out, changing content
    $(selector+ ' .splash_content').html(content[count]).fadeIn();
    $(selector+' .splash_controls a.selected').removeAttr("class");
    $(selector+' .splash_controls a[rel='+count+']').attr("class","selected");
    });

    return count;
}

// slider
var items = 0;
var content = new Array(3);
var control = '';
var count = 1;

$.get(xml, function(data)
{ // get contents from xml

    $(data).find('slide').each(function()
    {
        // populate array
        items++;
        var $slide = $(this); 
        content[items] = $slide.find('content').text();
        // create controls
        if(items==1) control = control + '<a href="#" class="selected" rel="'+items+'"></a>';
        else control = control + '<a href="#" rel="'+items+'"></a>';
    });

    $(selector+' .splash_content').html(content[count]);

    // add controls

    $(selector+' .splash_controls').html(control);
});

// control is clicked
$(selector+' .splash_controls a').live('click',function (){
    var slideID = $(this).attr('rel');
    count = Slide2Next(selector,count,content,items,slideID);
    clearInterval(slideInterval);
    slideInterval = setInterval(function() {count = Slide2Next(selector,count,content,items)}, 10000 );
    return false;
});

var slideInterval = setInterval(function() {count = Slide2Next(selector,count,content,items)}, 10000 );
}

问题来自编码的这一部分:

// control is clicked
$(selector+' .splash_controls a').live('click',function (){
    var slideID = $(this).attr('rel');
    count = Slide2Next(selector,count,content,items,slideID);
    clearInterval(slideInterval);
    slideInterval = setInterval(function() {count =    Slide2Next(selector,count,content,items)}, 10000 );
return false;
});

5 个答案:

答案 0 :(得分:2)

在jQuery 1.9中删除了

live。您需要使用on()代替。你的代码的立即修复就是这样做

$(document).on('click', selector+' .splash_controls a', function (){

但当然会导致文档中任何位置的所有事件被检查,这是次优的(也确切地说是实时工作的方式)。因此,如果这些锚点始终位于某个父容器内(当此代码运行时存在),则应该执行此操作

$('#parentContainer').on('click', selector+' .splash_controls a', function (){

答案 1 :(得分:1)

live()已被弃用,如上所述:

  

从jQuery 1.7开始,不推荐使用.live()方法。使用.on()附加事件处理程序。旧版jQuery的用户应该使用.delegate()而不是.live()。

所以试试on()

$(selector+' .splash_controls a').on('click',function (){
    var slideID = $(this).attr('rel');
    count = Slide2Next(selector,count,content,items,slideID);
    clearInterval(slideInterval);
    slideInterval = setInterval(function() {count =    Slide2Next(selector,count,content,items)}, 10000 );
    return false;
});

答案 2 :(得分:0)

试试这个

 $(selector+' .splash_controls a').on('click',function (){
    var slideID = $(this).attr('rel');
    count = Slide2Next(selector,count,content,items,slideID);
    clearInterval(slideInterval);
    slideInterval = setInterval(function() {count =    Slide2Next(selector,count,content,items)}, 10000 );
    return false;
});

答案 3 :(得分:0)

因为它没有; live在1.7中被弃用,AFAIK最终被移除。

http://api.jquery.com/category/removed/

答案 4 :(得分:0)

如果点击仍无效,您可以试试这个:

$(selector+' .splash_controls').on('click', 'a', function(){ *your stuff here* });

祝你好运!