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

jQuery中的insertBefore(),insertAfter(),after(),before()區(qū)別介紹

 更新時間:2016年09月01日 13:44:49   作者:小碼農(nóng)雯  
這篇文章主要介紹了jQuery中的insertBefore(),insertAfter(),after(),before()區(qū)別介紹的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

insertBefore():a.insertBefore(b)

       a在前,b在后,

       a:是一個選擇器,b:也是一個選擇器

<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>jqu</title>
<script type="text/javascript" src='jquery-2.2.0.min.js'></script>
</head>
<body>
<p class='p1'>p1:hello</p>
hello world
<p class='p2'>p2:wenwen</p>
hello wo
</body>
<script type="text/javascript">
$(function(){
$('.p2').insertBefore('.p1');
})
</script>
</html> 

得到:

p2:wenwen
p1:hello
hello world hello wo 

insertAfter():跟insertBefore()是一樣的道理

      a.insertAfter(b)

      a在后,b在前

現(xiàn)在是說before()

before():a.before()

     a是頁面上已有的選擇器,b是你需要添加的內(nèi)容,注意:是什么就是什么,會識別標簽,b不是一個選擇器

     a在后,b在前

<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>jqu</title>
<script type="text/javascript" src='jquery-2.2.0.min.js'></script>
</head>
<body>
<p class='p1'>p1:hello</p>
<p class='p2'>p2:wenwen</p>
</body>
<script type="text/javascript">
$(function(){
$('.p2').before('.p1');
})
</script>
</html> 

最后得到:

p1:hello
.p1
p2:wenwen 

看到嗎?.p1并不識別選擇器,直接就是字符串,在.p2選擇器的前面位置

after():跟before()是同理的,只是一個在前一個在后

我只是想說insertBefore(),insertAfter()跟before(),after()的區(qū)別,我感覺最大一個區(qū)別就是看你要用到的場景,你要是需要兩個選擇器的位置調(diào)換就用

insertBefore(),insertAfter()

要是需要一個選擇器跟一個文本進行調(diào)換位置就可以用before(),after();當然這個不只是調(diào)換位置啦

調(diào)換位置是說頁面上已經(jīng)存在的東西,這個方法里面也可以加頁面上沒有的東西,比如:

$('<p class='p3'>嘿嘿</p>').insertBefore('.p1');

以上所述是小編給大家介紹的jQuery中的insertBefore(),insertAfter(),after(),before()區(qū)別介紹,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論