jQuery | insertBefore()及其示例

这个 insertBefore() 是jQuery中的一个内置方法,用于在指定元素之前插入一些HTML内容。HTML内容将在每次出现指定元素之前插入。 语法:

null
$(content).insertBefore(target)

此处内容是需要在指定目标之前插入的HTML内容。 参数: 它接受一个参数“target”,该参数是要插入内容的目标。 返回类型: 它不返回任何值。

显示insertBefore()方法工作的jQuery代码:
代码#1:

<!DOCTYPE html>
< html >
< head >
< script
</ script >
< script >
$(document).ready(function() {
$("div").click(function() {
// insertBefore
$("< p >You should follow GeeksForGeeks</ p >").insertBefore("p");
});
});
</ script >
</ head >
< body >
< p >To learn jQuery </ p >
< div >Click here to complete</ div >
</ body >
</ html >


输出: 在单击div内容之前-

To learn jQuery
Click here to complete

单击div内容后-

You should follow GeeksForGeeks
To learn jQuery 
Click here to complete

代码#2:

<!DOCTYPE html>
< html >
< head >
< script
</ script >
< script >
$(document).ready(function() {
$("div").click(function() {
// insertBefore
$("< p >You should follow GeeksForGeeks</ p >").insertBefore("p");
});
});
</ script >
</ head >
< body >
< p >To learn jQuery </ p >
< p > To learn coding </ p >
< div >Click here to complete</ div >
</ body >
</ html >


输出: 在单击div内容之前-

To learn jQuery 
To learn coding 
Click here to complete

单击div内容后-

You should follow GeeksForGeeks
To learn jQuery 
You should follow GeeksForGeeks
To learn coding
Click here to complete
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享