jQuery中html()方法用法執行個體_jquery

來源:互聯網
上載者:User

本文執行個體講述了jQuery中html()方法用法。分享給大家供大家參考。具體分析如下:

此方法能夠設定和取得匹配元素的HTML內容,原來的內容將會被新設定的內容替換。

特別說明:

HTML內容就是內容中可以包含HTML標籤,並且能夠被瀏覽器渲染。
常值內容是先將內容中的HTML預定義字元轉換成html字元實體,這樣HTML標籤就不會被渲染。
文法結構一:

複製代碼 代碼如下:
$(selector).html()

此時方法不帶參數時候是取得第一個匹配元素的html內容。

此方法與text()方法沒有參數用法類似,但是還是有很大區別:

一.html()方法取得第一個匹配元素的內容,而text()方法是取得所有匹配元素包含的內容。
二.html()方法取得內容html內容,而text()方法取得是常值內容。

執行個體代碼:

複製代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>雲棲社區</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(){
    alert($("div").html());
  })
})
</script>
</head>
<body>
<div>
  <ul>
    <li><span>雲棲社區歡迎您</span></li>
  </ul>
</div>
<button>點擊查看效果</button>
</body>
</html>

以上代碼將返回div元素中的內容。
文法結構二:

複製代碼 代碼如下:
$(selector).html(content)

帶有參數的時候是設定所有匹配元素而的html內容。
此方法與text()方法帶有參數的用法類似,但是還是有很大的區別:
html()方法設定的是html內容,而text()方法設定的是常值內容。

執行個體代碼:

以下代碼將div中的html內容設定為"<b>我是重新設定後的內容</b>"。

複製代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>雲棲社區</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(){
    $("div").html("<b>我是重新設定後的內容</b>");
  })
})
</script>
</head>
<body>
<div>原來內容</div>
<button>點擊查看效果</button>
</body>
</html>

執行個體二:

複製代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>雲棲社區</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").html("<b>好好學習</b>");
    $(".text").text("<b>好好學習</b>");    
  })
})
</script>
</head>
<body>
<div class="html"></div>
<div class="text"></div>
<button>點擊查看效果</button>
</body>
</html>

通過此執行個體大家可以觀察一下html內容和常值內容的區別。

希望本文所述對大家的jQuery程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.