JQuery 方法

$.data()

data() 方法向被选元素附加数据,或者从被选元素获取数据。

1.从元素返回数据

$(selector).data(name)

2.向元素附件数据

$(selector).data(name,value)

3.使用对象向元素附件数据

$(selector).data(object)

<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("div").data("greeting", "Hello World");
});
$("#btn2").click(function(){
alert($("div").data("greeting"));
});
});
</script>
</head>
<body>

<button id="btn1">将数据附加到div元素</button><br>
<button id="btn2">获取数据附加到div元素</button>
<div></div>

/*若先点击bin2将会弹出undedined,顺序点击btn1,btn2,弹出Hellow World*/

 

$.each()

each() 方法为每个匹配元素规定要运行的函数。

$(selector).each(function(index,element))

function(index,element) :必须,为每个匹配元素规定执行的函数

  • index - 选择器的 index 位置。
  • element - 当前的元素(也可使用 "this" 选择器)。

$.index()

index() 方法返回指定元素相对于其他指定元素的 index 位置。从0开始

这些元素可通过 jQuery 选择器或 DOM 元素来指定。

注意:如果未找到元素,index() 将返回 -1。

$(selector).index()

posted @ 2019-03-15 17:50  滥好人儿  阅读(96)  评论(0编辑  收藏  举报