Jquery map()each()及html()、text()、value()
来源:互联网 发布:图书管理系统php 编辑:程序博客网 时间:2024/06/04 18:32
1、Map方法的作用
//作用 1.遍历 数组,回调函数形参为:(被遍历的元素,被遍历的元素下标)
//$.map(arr, function (ele, index) { alert(index + ":" + ele); });
//作用 1的模拟,调用自定义myMap方法 来 遍历 arr数组,同时传入 一个 【回调函数】(用户定义,系统调用)
//myMap01(arr, function (ele, index) { alert(index + ":" + ele); });
//$.map(arr, function (ele,index) { alert(this); });
//作用 2.遍历 数组,返回新数组
//var newArr = $.map(arr, function (ele, index) { return ele * 2; });
//alert(newArr);
//var newArr = myMap02(arr, function (ele, index) { return ele * 2; });
//alert(newArr);
//作用 3.遍历 对象的属性
var obj = { id: 1, name: "james" };
//$.map(obj, function (value, key) {
// alert(key + ":" + value);
//})
myMap03(obj, function (value, key) {
alert(key + ":" + value);
})
})
//map方法模拟 遍历数组
function myMap01(arr, callback) {
//遍历 数组 的 下标
for (var index in arr)
{
var item =arr[index];//通过下标,获取当前循环的元素
callback(item, index);//调用回调函数,并传入 数组元素 和 数组下标
}
};
//map方法模拟 返回新数组
function myMap02(arr, callback) {
var arrNew = new Array(); // a,b => a,b,c
for (var i = 0; i < arr.length; i++) {
var item = arr[i];//获取当前循环的元素
arrNew[arrNew.length] = callback(item, i);//调用回调函数,并把返回值 追加到 新数组中
}
return arrNew;
};
//map方法模拟 遍历对象属性
function myMap03(obj, callback)
{
for (var key in obj)//遍历 属性名
{
var value = obj[key];//根据 对象的 属性名 获取 对象的属性值
callback(value, key);//调用回调函数,并传入 属性名 和 属性值
}
}
2、each方法的作用
$(function () {
var obj = { id: 1, name: "james" };
//方法1: 遍历对象 的属性 和 属性值 其中的this 是对应的 属性值
//$.each(obj, function (key,value) {
// alert(key + " : " + value + " , this=" + this);
//});
//var fun = new Function(); 凡是 Function的实例,都有call,apply等函数对象特有的成员
//调用 模拟的 each 方法
//myEach01(obj, function(key, value) {
// alert(key + " : " + value + " , this=" + this);
//});
//方法2:遍历 数组 其中的 this 是数组对应元素 的值
var arr = [17, 20, 3, 64];
$.each(arr, function (index, value) {
alert(index + " : " + value + ", this =" + this);
});
})
3、html()、text()、value()
//1.返回 Jquery对象里 dom数组 的第一个元素 的 innerHTML
//var strHtml = $("div").html();
//alert(strHtml);
//2.返回 Jquery对象里 dom数组 的每个元素 的 innerText 的拼接字符串
//var strHtml = $("div").text();
//alert(strHtml);
//3.返回 Jquery对象里 dom数组 的第一个元素 的 value
var strVal = $("input[type=text]").val();
alert(strVal);
- Jquery map()each()及html()、text()、value()
- jquery $.each(box,function(attr,value){}) 与 $().each(function(index,element){})
- jquery $.each,$.map
- jQuery获取Select选择的Text和Value(详细)
- web-jQuery中html() .value().text()的区别
- jQuery:html(),text(),val() JS:innerHTML,innerText,value
- jQuery函数map()和each()介绍及异同点分析
- 多级联动,下拉列表取text及value值的两种方法(传统方法+JQuery方法)
- jquery之$.each()
- jquery value text
- jQuery .html() .each()
- 遍历(一)jquery $().each和$.each()
- 遍历(一)jquery $().each和$.each()
- jQuery选择器(重要) 基本选择器;jquery的html方法,text方法,each方法,index方法
- jQuery中map和each的用法(jquery-1.12.1.js)
- jquery的each,map,has
- jQuery获取Select选中的Text和Value(jquery学习备忘录)
- jQuery获取Select选中的Text和Value(jquery学习备忘录)
- hadoop 的MapReduce原理与心得!!!
- Xcode6.2正式版 下载地址(网盘)
- Linux下静态链接库与动态链接库的区别
- 代码编辑神器VIM
- jquery:Deffered详解
- Jquery map()each()及html()、text()、value()
- 快速排序实现细节的思考
- 双机热备
- Android核心分析
- nginx的安装
- SNMP从入门到开发:进阶篇
- Model-1模式的分页逻辑
- ubuntu 在虚拟机环境下显示联网但无法连接外网的解决办法(利用虚拟网络编辑器)
- css