jQuery prependTo()的例子
prependTo()方法是jQuery内置的方法,用于在所选元素的开头插入HTML元素或一些内容。
语法:
$(content).prepend(selector)
参数:该函数接受上面提到的和下面描述的两个参数。
- content。这是一个必要参数,用于指定要插入的内容。
- selector。这是一个必要的参数,用于指定要预置内容的元素。
返回值:该方法返回选定的元素,以及prependTo方法所做的具体改动。
下面的例子说明了jQuery中的prependTo()方法。
示例:
<!DOCTYPE html>
<html>
<head>
<title>The prependTo Method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!-- jQuery code to show the working of this method -->
<script>
(document).ready(function(){
("button").click(function(){
$("<span>Welcome to </span>").prependTo("p");
});
});
</script>
<style>
div{
width: 350px;
min-height: 180px;
font-weight: bold;
padding:20px;
font-size: 25px;
border: 2px solid green;
}
</style>
</head>
<body>
<div>
<p>GeeksforGeeks!</p>
<!-- Click on this button to see the change -->
<button >Click Here!</button>
</div>
</body>
</html>
输出: