欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery中data()方法用法實(shí)例

 更新時(shí)間:2014年12月27日 14:51:55   投稿:shichen2014  
這篇文章主要介紹了jQuery中data()方法用法,實(shí)例分析了data()方法向匹配元素附加數(shù)據(jù)及獲取數(shù)據(jù)的使用技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了jQuery中data()方法用法。分享給大家供大家參考。具體分析如下:

此方法可以向匹配元素附加數(shù)據(jù),或者從匹配元素獲取數(shù)據(jù)。

語(yǔ)法結(jié)構(gòu)一:

復(fù)制代碼 代碼如下:
$(selector).data(name,value)

參數(shù)列表:

參數(shù) 描述
name 存儲(chǔ)的數(shù)據(jù)名稱(chēng)。
value 將要存儲(chǔ)的任意數(shù)據(jù)。

實(shí)例代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>data()函數(shù)-腳本之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#add").click(function(){
    $("div").data("mydata","腳本之家歡迎您");
  })
  $("#show").click(function(){
    $("div").text($("div").data("mydata"));
  })
})
</script>
</head>
<body>
<div></div>
<button id="add">向元素添加數(shù)據(jù)</button>
<button id="show">顯示添加的數(shù)據(jù)</button>
</body>
</html>

以上代碼能夠在匹配的div元素上附加名稱(chēng)mydata,值為“腳本之家歡迎您”的數(shù)據(jù),然后利用數(shù)據(jù)名稱(chēng)返回。

語(yǔ)法結(jié)構(gòu)二:

從匹配元素中返回指定數(shù)據(jù)名稱(chēng)的附加的數(shù)據(jù)。

復(fù)制代碼 代碼如下:
$(selector).data(name)

參數(shù)列表:

參數(shù) 描述
name 存儲(chǔ)的數(shù)據(jù)名稱(chēng)。

實(shí)例代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>data()函數(shù)-腳本之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#add").click(function(){
    $("div").data("mydata","腳本之家歡迎您");
  })
  $("#show").click(function(){
    $("div").text($("div").data("mydata"));
  })
})
</script>
</head>
<body>
  <div></div>
  <button id="add">向元素添加數(shù)據(jù)</button>
  <button id="show">顯示添加的數(shù)據(jù)</button>
</body>
</html>

以上代碼能夠在匹配的div元素上附加名稱(chēng)mydata,值為“腳本之家歡迎您”的數(shù)據(jù),然后利用數(shù)據(jù)名稱(chēng)返回。

語(yǔ)法結(jié)構(gòu)三:

使用鍵/值對(duì)對(duì)象向匹配元素添加數(shù)據(jù)。

復(fù)制代碼 代碼如下:
$(selector).data(properties)

參數(shù)列表:

參數(shù) 描述
properties 一個(gè)用于設(shè)置數(shù)據(jù)的鍵/值對(duì)。

實(shí)例代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>data()函數(shù)-腳本之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#add").click(function(){
    $("div").data("mydata",{username:"daoliang"});
  })
  $("#show").click(function(){
    alert($("div").data("mydata").username);
  })
})
</script>
</head>
<body>
<div></div>
<button id="add">把數(shù)據(jù)添加元素</button>
<button id="show">獲取添加到元素的數(shù)據(jù)</button>
</body>
</html>

以上代碼能夠以鍵/值對(duì)方式為div附加名稱(chēng)為mydata的數(shù)據(jù),然后通過(guò)數(shù)據(jù)名和鍵取得附加的數(shù)據(jù)值。

語(yǔ)法結(jié)構(gòu)四:

使用對(duì)象方式為指定元素附加數(shù)據(jù)。

復(fù)制代碼 代碼如下:
$(selector).data(object)

參數(shù)列表:

參數(shù) 描述
object 一個(gè)用于設(shè)置數(shù)據(jù)的對(duì)象。

實(shí)例代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>data()函數(shù)-腳本之家</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  var mytest=new Object();
  mytest.first="腳本之家歡迎您";
  mytest.second="JQuery專(zhuān)區(qū)";
  $("#add").click(function(){
    $("div").data(mytest);
  })
  $("#show").click(function(){
    alert($("div").data("second"));
  })
});
</script>
</head>
<body>
  <div></div>
  <button id="add">把數(shù)據(jù)添加元素</button>
  <button id="show">獲取添加到元素的數(shù)據(jù)</button>
</body>
</html>

以上代碼以對(duì)象方式附加數(shù)據(jù),并且取得附加的數(shù)據(jù)值。

希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 使用jQuery實(shí)現(xiàn)一個(gè)類(lèi)似GridView的編輯,更新,取消和刪除的功能

    使用jQuery實(shí)現(xiàn)一個(gè)類(lèi)似GridView的編輯,更新,取消和刪除的功能

    在項(xiàng)目中遇到這樣的需求當(dāng)用戶(hù)點(diǎn)擊編輯時(shí),在點(diǎn)擊行下動(dòng)態(tài)產(chǎn)生一行,編輯銨鈕變?yōu)閐isabled,新產(chǎn)生的一行有更新和取消的銨鈕,點(diǎn)擊“取消”銨鈕,刪除剛剛動(dòng)態(tài)產(chǎn)生的行,編輯銨鈕狀態(tài)恢復(fù)。下面小編給大家分享實(shí)例代碼,一起看看吧
    2017-03-03
  • jQuery學(xué)習(xí)筆記(2)--用jquery實(shí)現(xiàn)各種模態(tài)提示框代碼及項(xiàng)目構(gòu)架

    jQuery學(xué)習(xí)筆記(2)--用jquery實(shí)現(xiàn)各種模態(tài)提示框代碼及項(xiàng)目構(gòu)架

    想實(shí)現(xiàn)一個(gè)模態(tài)的框框,找了很多的jquery插件,都沒(méi)有碰到自己滿(mǎn)意的于是自己摸索了一個(gè),接下來(lái)為大家介紹下實(shí)現(xiàn)的思路及代碼,希望對(duì)你有所幫助
    2013-04-04
  • jquery js 重置表單 reset()具體實(shí)現(xiàn)代碼

    jquery js 重置表單 reset()具體實(shí)現(xiàn)代碼

    我們希望表單提交以后,能reset,由于jquery沒(méi)有這個(gè)方法,所以只能采用其他的方法來(lái)實(shí)現(xiàn)了,具體如下,有此需求的朋友可以參考下,希望對(duì)大家有所幫助
    2013-08-08
  • jQuery+CSS3實(shí)現(xiàn)3D立方體旋轉(zhuǎn)效果

    jQuery+CSS3實(shí)現(xiàn)3D立方體旋轉(zhuǎn)效果

    這篇文章主要介紹了jQuery結(jié)合CSS3來(lái)制作3D立方體旋轉(zhuǎn)效果,切換圖片時(shí)呈現(xiàn)3D立體圖片畫(huà)廊特效,需要的朋友可以參考下
    2015-11-11
  • Jquery iframe內(nèi)部出滾動(dòng)條

    Jquery iframe內(nèi)部出滾動(dòng)條

    Jquery 操作頁(yè)面中iframe自動(dòng)跟隨窗口大小變化,而不出現(xiàn)滾動(dòng)條,只在iframe內(nèi)部出滾動(dòng)條
    2010-02-02
  • jQuery 遍歷map()方法詳解

    jQuery 遍歷map()方法詳解

    大家都知道m(xù)ap()方法主要用來(lái)遍歷操作數(shù)組和對(duì)象,這篇文章將給大家詳細(xì)介紹關(guān)于jQuery遍歷map()方法的內(nèi)容,文章給出了詳細(xì)的示例代碼,對(duì)大家的理解和學(xué)習(xí)很有幫助,有需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • Jquery1.9.1源碼分析系列(十五)動(dòng)畫(huà)處理之外篇

    Jquery1.9.1源碼分析系列(十五)動(dòng)畫(huà)處理之外篇

    這篇文章主要介紹了Jquery1.9.1源碼分析系列(十五)動(dòng)畫(huà)處理之外篇 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • checkbox:click事件觸發(fā)span元素內(nèi)容改變的方法

    checkbox:click事件觸發(fā)span元素內(nèi)容改變的方法

    下面小編就為大家?guī)?lái)一篇checkbox:click事件觸發(fā)span元素內(nèi)容改變的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • jQuery3.0中的buildFragment私有函數(shù)詳解

    jQuery3.0中的buildFragment私有函數(shù)詳解

    在 jQuery3.0中,buildFragment 是一個(gè)私有函數(shù),用來(lái)構(gòu)建一個(gè)包含子節(jié)點(diǎn) fragment 對(duì)象。下文給大家介紹jQuery3.0中的buildFragment私有函數(shù)詳解,對(duì)jquery3.0 buildfragment相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-08-08
  • jQuery 移動(dòng)端artEditor富文本編輯器

    jQuery 移動(dòng)端artEditor富文本編輯器

    這篇文章主要介紹了jQuery 移動(dòng)端artEditor富文本編輯器 的相關(guān)資料,需要的朋友可以參考下
    2016-01-01

最新評(píng)論