jquery - attr() 值回调有什么用途

查看:74
本文介绍了jquery - attr() 值回调有什么用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

值回调就是给参数传入一个函数。每个匹配的元素都调用一次该函数。。。我不是很明白。。比如什么情况下用这个号。。最好给个实际运用的案例

解决方案

这里不是说的挺明白的么 http://api.jquery.com/attr/#a...

function
Type: Function( Integer index, String attr ) => String or Number
A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old attribute value as arguments.

比如你有个<div id="hello" world="2"></div>
你想每次都给world自增1
可以这么做

jQuery('#hello').attr('world',function(i,old){
    return ~~old+1;
});

ps:你不问我还不知道有这个回调呢,哈哈

这篇关于jquery - attr() 值回调有什么用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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