本文實(shí)例講述了jQuery中before()方法用法。分享給大家供大家參考。具體分析如下:
此方法可向每個(gè)匹配元素的外部的前部追加HTML內(nèi)容。
特別說(shuō)明:
此方法是追加內(nèi)容,也就是原來(lái)的內(nèi)容還在。
HTML內(nèi)容就是內(nèi)容中可以包含HTML標(biāo)簽,并且能夠被瀏覽器渲染。
文本內(nèi)容是先將內(nèi)容中的HTML預(yù)定義字符轉(zhuǎn)換成html字符實(shí)體,這樣HTML標(biāo)簽就不會(huì)被渲染。
語(yǔ)法結(jié)構(gòu):
實(shí)例代碼:
實(shí)例一:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.ythuaji.com.cn/" />
<title>服務(wù)器之家</title>
<style type="text/css">
div
{
height:150px;
width:150px;
background-color:green;
margin-top:10px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").before("<b> 好好學(xué)習(xí)</b>");
})
</script>
</head>
<body>
<div></div>
</body>
</html>
在原來(lái)div內(nèi)容的前面追加內(nèi)容。
實(shí)例二:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.ythuaji.com.cn/" />
<title>服務(wù)器之家</title>
<style type="text/css">
div
{
height:150px;
width:150px;
background-color:green;
margin-top:10px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$(".html").before("<b>好好學(xué)習(xí)</b>");
$(".text").text("<b>好好學(xué)習(xí)</b>");
})
})
</script>
</head>
<body>
<div class="html"></div>
<div class="text"></div>
<button>點(diǎn)擊查看效果</button>
</body>
</html>
通過(guò)此實(shí)例大家可以觀察一下HTML內(nèi)容和文本內(nèi)容的區(qū)別。
希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。