国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery中detach()方法用法實例

2019-11-20 13:36:17
字體:
供稿:網(wǎng)友

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

此方法從DOM中刪除所有匹配的元素。

說明:detach()方法不會把匹配的元素從jQuery對象中刪除,因而可以在將來再使用這些匹配的元素,與remove()不同的是,所有綁定的事件、附加的數(shù)據(jù)等都會保留下來。

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

復(fù)制代碼 代碼如下:
$(selector).detach(expr)

參數(shù)列表:

參數(shù) 描述
expr 可選。用于篩選被刪除元素的jQuery表達(dá)式。
實例代碼:

實例一:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.survivalescaperooms.com/" />
<title>武林網(wǎng)</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
  $("button").click(function(){
    $("div").detach("#first");
  });
})
</script> 
</head>
<body>
  <div id="first">歡迎來到武林網(wǎng)</div>
  <div>武林網(wǎng)歡迎您</div>
  <button>點擊查看效果</button>
</body>
</html>

以上代碼能夠刪除div集合中id值為first的div。

實例二:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.survivalescaperooms.com/" />
<title>武林網(wǎng)</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
  $("button").click(function(){
    $("div").detach();
  });
})
</script>
</head>
<body>
<div id="first">歡迎來到武林網(wǎng)</div>
<div>武林網(wǎng)歡迎您</div>
<button>點擊查看效果</button>
</body>
</html>

如果方法沒有參數(shù),那么將會刪除所有匹配元素。

實例三:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://m.survivalescaperooms.com/" />
<title>武林網(wǎng)</title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
  $("#btd").click(function(){
    var a=$("div");
    a.detach("#first");
    $("#btn").click(function(){
      alert(a.length);
    });
  });
})
</script> 
</head>
<body>
  <div id="first">歡迎來到武林網(wǎng)</div>
  <div id="second">武林網(wǎng)歡迎您</div>
  <button id="btd">刪除div效果</button>
  <button id="btn">查看刪除操作后div的數(shù)量</button>
</body>
</html>

以上代碼的運行結(jié)果可以看出,此方法并沒有將div從jquery對象中刪除。

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 太湖县| 清水河县| 吴堡县| 桐乡市| 杂多县| 广安市| 开封县| 区。| 嘉峪关市| 乌兰浩特市| 东平县| 道孚县| 陆川县| 兴义市| 岑溪市| 平邑县| 皮山县| 马鞍山市| 寻乌县| 友谊县| 白银市| 德格县| 宁夏| 阳城县| 海原县| 石泉县| 精河县| 临夏县| 长治县| 康保县| 襄垣县| 巫山县| 东明县| 永春县| 新宾| 马鞍山市| 资溪县| 东方市| 昆明市| 沈阳市| 鸡西市|