javascript数据结构,jQuery图片加载失败替换默认图片方法汇总

JavaScript数据结构之双向链表和双向循环链表的实现,javascript数据结构

双向链表和普通链表的区别在于,在链表中,一个节点只有链向下一个节点的链接,而在双向链表中,链接是双向的:一个链向下一个元素,另一个链向前一个元素。

双向链表提供了两种迭代列表的方法:从头到尾,或者反过来。我们也可以访问一个特定节点的下一个或前一个元素。在单向链表中,如果迭代列表时错过了要找的元素,就需要回到列表起点,重新开始迭代。这是双向链表的一个优点。

双向链表:单向链表只能向着一个方向遍历链表节点,而在节点指针域中增加了前向指针的双向链表,则可以向着两个方向遍历节点。这使得双向链表也可以在任何一个节点遍历整个链表。

function DoublyLinkedList() { 
  var Node = function(element) { 
    this.element = element; 
    this.next = null; 
    this.prev = null; 
  }; 

  var length = 0, 
    head = null, 
    tail = null; 

  this.append = function(element){ 
    var node = Node(element), 
      current, 
      previous; 

    if(!head){ 
      head = node; 
      tail = node; 
    }else{ 
      current = head; 
      while(current){ 
        previous = current; 
        current = current.next; 
      } 

      node.next = current; 
      current.prev = node; 
      previous.next = node; 
      node.prev = previous; 
    } 

    length++; 
    return true; 
  } 

  this.insert = function(position,element){ 
    if(position > -1 && position < length){ 
      var node = new Node(element), 
        current = head, 
        previous, 
        index = 0; 

      if(position === 0){ 

        if(!head){ 
          head = node; 
          tail = node; 
        }else{ 
          node.next = current; 
          current.prev = node; 
          head = node; 
        } 

      }else if (position === length -1){ 
        current = tail; 
        current.next = node; 
        node.prev = current; 
      }else { 
        while(index++ < position){ 
          previous = current; 
          current = current.next; 
        } 
        node.next = current; 
        previous.next = node; 
        current.prev = node; 
        node.prev = previous; 
      } 

      length++; 
      return true; 
    }else{ 
      return false; 
    } 
  }; 

  this.removeAt = function(position){ 
    if(position > -1 && position < length){ 
      var current = head, 
        index = 0, 
        previous; 

      if (position === 0) { 
        head = current.next; 

        if(length === 1){ 
          tail = null; 
        }else{ 
          head.prev = null; 
        } 
      }else if(position === length - 1){ 
        current = tail; 
        tail = current.prev; 
        tail.next = null; 
      } else{ 
        while(index++ < position){ 
          previous = current; 
          current = current.next; 
        } 

        previous.next = current.next; 
        current.next.prev = previous; 
      }; 
      length-- ; 

      return current.element; 
    }else{ 
      return false; 
    } 
  }; 

  this.remove = function(element){ 
    var current = head, 
      previous; 

    if(current.element === element){ 
      head = current.next; 
    } 
    previous = current; 
    current = current.next; 

    while(current){ 
      if (current.element = element) { 
        previous.next = current.next; 
        current.next.prev = previous; 
      }else{ 
        previous = current; 
        current = current.next; 
      } 
    } 
    return false; 
  }; 

  this.remove = function(){ 
    if (length === 0) { 
      return false; 
    }; 

    var current = head, 
      previous; 

    if(length === 1){ 
      head = null; 
      tail = null; 
      length--; 
      return current.element; 
    } 

    while(current){ 
      previous = current; 
      current = current.next; 
    } 

    previous.next = null; 
    length--; 
    return current.element; 
  }; 

  this.indexOf = function(element){ 
    var current = head, 
      index = 0; 

    while(current && index++ < length){ 
      if (current.element === element) { 
        return index; 
      }; 
      current = current.next; 
    } 

    return false; 
  }; 

  this.isEmpty = function(){ 
    return length === 0; 
  }; 

  this.size = function(){ 
    return length; 
  }; 

  this.toString = function(){ 
    var current = head, 
      string = ''; 

    while(current){ 
      string += current.element; 
      current = current.next; 
    } 
    return string; 
  }; 

  this.getHead = function(){ 
    return head; 
  }; 

  this.getTail = function(){ 
    return tail; 
  }; 
} 

双向循环链表:将双向链表的头尾指针相连,就构成了双向循环链表。这种链表从任意一个节点都可以同时向两个方向进行节点遍历,查询节点的速度也是最快的。

/*双向循环链表*/ 
function DoublyCircularLinkedList(){ 
  var Node = function(element){ 
    this.element = element; 
    this.next = null; 
    this.prev = null; 
  }; 

  var length = 0, 
    head = null, 
    tail = null; 

  this.append = function(element){ 
    var node = new Node(element), 
      current, 
      previous; 

    if (!head) { 
      head = node; 
      tail = node; 
      head.prev = tail; 
      tail.next = head; 
    }else{ 
      current = head; 

      while(current.next !== head){ 
        previous = current; 
        current = current.next; 
      } 

      current.next = node; 
      node.next = head; 
      node.prev = current; 
    }; 

    length++; 
    return true; 
  }; 

  this.insert = function(position, element){ 
    if(position >= 0 && position <= length){ 
      var node = new Node(element), 
        index = 0, 
        current = head, 
          previous; 

      if(position === 0){ 

        if(!head){ 

          node.next = node; 
          node.tail = node; 
          head = node; 
          tail = node; 

        }else{ 

          current.prev = node; 
          node.next = current; 
          head = node; 
          node.prev = tail; 

        } 

      }else if(position === length){ 
        current = tail; 

        current.next = node; 
        node.prev = current; 
        tail = node; 
        node.next = head; 
      }else{ 

        while(index++ < position){ 
          previous = current; 
          current = current.next; 
        } 

        current.prev = node; 
        node.next = current; 
        previous.next = node; 
        node.prev = previous; 

      } 

      length++; 
      return true; 
    }else{ 
      return false; 
    } 
  }; 

  this.removeAt = function(position){ 
    if(position > -1 && position < length){ 

      var current = head, 
        index = 0, 
        previous; 

      if(position === 0){ 

        current.next.previous = tail; 
        head = current.next; 

      }else if(position === length - 1){ 

        current = tail; 

        current.prev.next = head; 
        head.prev = current.prev; 
        tail = current.prev; 
      }else{ 

        while(index++ < position){ 
          previous = current; 
          current = current.next; 
        } 

        previous.next = current.next; 
        current.next.prev = previous; 

      } 

      length--; 
      return true; 
    }else{ 
      return false; 
    } 
  }; 

  this.remove = function(element){ 
    var current = head, 
      previous, 
      indexCheck = 0; 

    while(current && indexCheck < length){ 
      if(current.element === element){ 
        if(indexCheck === 0){ 
          current.next.prev = tail; 
          head = current.next; 
        }else{ 
          current.next.prev = previous; 
          previous.next = current.next; 
        } 
        length--; 
        return true; 
      } 

      previous = current; 
      current = current.next; 
      indexCheck++; 
    } 

    return false; 
  }; 

  this.remove = function(){ 
    if(length === 0){ 
      return false; 
    } 

    var current = head, 
      previous, 
      indexCheck = 0; 

    if(length === 1){ 
      head = null; 
      tail = null; 
      length--; 
      return current.element; 
    } 

    while(indexCheck++ < length){ 
      previous = current; 
      current = current.next; 
    } 

    previous.next = head; 
    tail = previous.next; 
    length--; 
    return current.element; 
  }; 

  this.indexOf = function(element){ 
    var current = head, 
      index = 0; 

    while(current && index++ < length){ 
      if(current.element === element){ 
        return index; 
      } 
      current = current.next; 
    } 

    return false; 
  }; 

  this.toString = function(){ 
    var current = head, 
      indexCheck = 0, 
      string = ''; 

    while(current && indexCheck < length){ 
      string += current.element; 
      indexCheck++; 
      current = current.next; 
    }   

    return string; 
  }; 

  this.isEmpty = function(){ 
    return length === 0; 
  }; 

  this.getHead = function(){ 
    return head; 
  }; 

  this.getTail = function(){ 
    return tail; 
  }; 

  this.size = function(){ 
    return length; 
  }; 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持帮客之家。

双向链表和普通链表的区别在于,在链表中,一个节点只有链向下一…

jQuery图片加载失败替换默认图片方法汇总,jquery汇总

本文主要讨论页面中图片加载失败后替换默认图片的几种方式

重点来了:一定要记住error事件不冒泡。

相关的知识点:jquery的ready方法、$(“img”).error()、img的complete属性、插件imagesLoaded、事件委托、事件捕获和图片预加载的方法等

1. 图片加载失败替换为默认图片

1.1 给图片绑定error事件

当图片加载失败时会触发error事件

$("img").on("error", function () {
  $(this).attr("src", "../img/img.jpg");
});
  • 不建议事件事件属性onerror,你懂的~O.o
  • 如果是动态添加的图片,还要重新绑定这个事件。像click事件这些可以利用事件委托的方式,但是事件委托是利用事件冒泡的思路来做的,而error事件不支持冒泡
  • 不建议在ready方法中给img绑定error事件,可能img加载图片失败时,img并没有绑定error事件,就会导致没有替换为默认图片

1.2 利用complete属性来判断

当图片加载失败时complete属性值为false,加载成功时true

$("img").each(function () {

  if (!this.complete || (typeof this.naturalWidth == "undefined" && this.naturalWidth == 0) || !this.src) {
    $(this).attr("src", "../img/img.jpg");
  }
});
  • 如果是动态添加的图片,还是要重新判断的
  • 可以在一点时间后再来判断,不然刚新增图片,图片资源可能还没有请求完就用这个方式来判断会有问题的
  • HTML 5中,新增了两个用来判断图片的宽度和高度的属性,分别为
    naturalWidth
    和naturalHeight属性(必须在图片完全下载到客户端浏览器才能判断)
  • img的onreadystatechange这个属性不讨论,有浏览器差异性

1.3 利用error事件捕获来处理(全局判断,动态添加的元素也可以-最优解)

document.addEventListener("error", function (e) {

  var elem = e.target;
  if (elem.tagName.toLowerCase() == 'img') {
    elem.src = "../img/img.jpg";
  }
}, true);

可以监听到动态产生的img标签

1.4 利用插件imagesLoaded提供的方法来处理

imagesLoaded主要用来在手机端瀑布流方式来加载图片,也可以用来处理图片加载失败替换为默认图片

// 用的是jQuery的deferred来实现的
$('img').imagesLoaded()
    .always(function (instance) { // always事件,在所有图片都加载完成(成功与否不论)时触发
      console.log('all images loaded');
    })
    .done(function (instance) { // done事件,在所有图片都加载成功时触发
      console.log('all images successfully loaded');
    })
    .fail(function (instance) { // fail事件,在所有图片都加载完成,并且至少有一张图片加载失败时触发
      console.log('all images loaded, at least one is broken');
    })
    .progress(function (instance, image) { // progress事件,在每一张图片加载完成时都触发一次
      var result = image.isLoaded ? 'loaded' : 'broken'; // 判断当前图片加载成功与否
      image.img.src = image.isLoaded ? image.img.src : "../img/img.jpg";
      console.log('image is ' + result + ' for ' + image.img.src);
    });

如果是动态添加的图片,还是要重新判断的

2. 图片预加载的方法

// 实现预加载图片,传入的参数是数组,数组的每一项为该图片的地址
function preloadimages(arr) {
  var newimages = [];
  var arr = (typeof arr != "object") ? [arr] : arr; // 确保参数总是数组
  for (var i = 0,len = arr.length; i < len; i++) {
    newimages[i] = new Image();
    newimages[i].src = arr[i];
  }
}

总结

以上所述是小编给大家介绍的jQuery图片加载失败替换默认图片方法汇总,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对帮客之家网站的支持!

本文主要讨论页面中图片加载失败后替换默认图片的几种方式
重点来了:一定要记住…

浅谈vue,angular,react数据双向绑定原理分析,angularreact

传统做法

前端维护状态,手动操作DOM更新视图。前端框架对服务器数据通过模版进行渲染。当用户产生了一个动作之后,我们通过document.getElementBy…
手动进行DOM更新。
框架帮忙分离数据和视图,后续状态更新需要手动操作DOM,因为框架只管首次渲染,不追踪状态监听变化。

双向数据绑定

发表评论

电子邮件地址不会被公开。 必填项已用*标注

标签:
网站地图xml地图