jQuery中insertBefore()方法用法实例
本文实例讲述了jQuery中insertBefore()方法用法。分享给大家供大家参考。具体分析如下:
此方法把匹配的元素插入到另一个指定的元素集合的前面。
insertBefore()方法与insertAfter()相反。
语法结构:
$(selector).insertBefore(content)
参数列表:
实例代码:
<!DOCTYPEhtml> <html> <head> <metacharset="utf-8"> <metaname="author"content="https://www.nhooo.com/"/> <title>insertBefore()函数-毛票票</title> <styletype="text/css"> p{ width:100px; height:100px; background-color:red; } div{ width:200px; height:200px; background-color:blue; } </style> <scripttype="text/javascript"src="mytest/jQuery/jquery-1.8.3.js"></script> <scripttype="text/javascript"> $(document).ready(function(){ $("#bt").click(function(){ $("div").insertBefore("p"); }) }); </script> </head> <body> <p></p> <div></div> <inputtype="button"value="点击查看效果"id="bt"/> </body> </html>
以上代码可以将div元素插入到p元素的前面。
需要特别注意的是,这里的插入是将匹配元素挪动到指定元素的前面,而不是复制一个新的。
希望本文所述对大家的jQuery程序设计有所帮助。