jQuery.one()和jQuery.on()之间的区别 [英] Difference between jQuery.one() and jQuery.on()

查看:156
本文介绍了jQuery.one()和jQuery.on()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有多张图像.为了检测出损坏的图像,我使用了在SO上找到的图像.

I have multiple images on my page. To detect broken images , I used this found on SO.

$('.imgRot').one('error',function(){
    $(this).attr('src','broken.png');
});

这在我了解的第一张图片上效果很好.但是当我将其更改为

This works fine on the first image which I understand. But when I change this to

$('.imgRot').on('error',function(){
    $(this).attr('src','broken.png');
});

它不适用于任何图像.有人可以告诉我为什么吗?

it does not work on any of the images . Could someone tell me why ?

推荐答案

社区Wiki:此通用答案不会对OP所发布的问题产生影响,而是与标题相关.

one() on()的概念可以用以下代码解释.

The concept of one() and on() can be explained with the below code.

one()功能在第一次出现后自动移至关闭状态.

one() function is automatically moved to off state after first instance of occurance.

on()与one()相同,但是需要将其手动设置为关闭状态,否则实例数没有限制.

on() is identical to one() but it needs to be manually put to off state otherwise the number of instances has no limit.

var i = 1;
$('.one').one('click', function() {

  $(this).text('I am clickable only once: ' + i);
  i++;
});

var j = 1;
$('.multiple').on('click', function() {

  $(this).text('I was clicked ' + j + ' times');
  j++;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="one">Click me</div>
<div class="multiple">Click me</div>

这篇关于jQuery.one()和jQuery.on()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆