本文实例讲述了jQuery中insertBefore()方法用法。分享给大家供大家参考。具体分析如下:
此方法把匹配的元素插入到另一个指定的元素集合的前面。
insertBefore()方法与insertAfter()相反。
语法结构:
$(selector).insertBefore(content)
参数列表:
参数 |
描述 |
selector |
定义要被插入的内容。 |
content |
定义在哪些元素之前插入内容。 |
实例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset=\" utf-8\">
<meta name=\"author\" content=\"http://www.phpstudy.net/\" />
<title>insertBefore()函数-phpstudy</title>
<style type=\"text/css\">
p{
width:100px;
height:100px;
background-color:red;
}
div{
width:200px;
height:200px;
background-color:blue;
}
</style>
<script type=\"text/javascript\" src=\"mytest/jQuery/jquery-1.8.3.js\"></script>
<script type=\"text/javascript\">
$(document).ready(function(){
$(\"#bt\").click(function(){
$(\"div\").insertBefore(\"p\");
})
});
</script>
</head>
<body>
<p></p>
<div></div>
<input type=\"button\" value=\"点击查看效果\" id=\"bt\" />
</body>
</html>
以上代码可以将div元素插入到p元素的前面。
需要特别注意的是,这里的插入是将匹配元素挪动到指定元素的前面,而不是复制一个新的。
希望本文所述对大家的jQuery程序设计有所帮助。
本文地址:https://www.stayed.cn/item/14995
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我