首页 > Web开发 > 详细

jQuery html() example

时间:2015-06-14 01:46:20      阅读:143      评论:0      收藏:0      [点我收藏+]

 

jQuery html() is used to get the html contents of the first element of the matched elements; While html(‘new html content’) is used to replace or set the html contents of all the matched elements.

For example, two div elements that contains same class name “AClass”.

<div class="AClass">ABC 1</div>
<div class="AClass">ABC 2</div>

  

1. $(‘.AClass’).html()

This will get the “ABC 1” only, the second matched element “ABC 2″ will be ignore.

 

2. $(‘.AClass’).html(‘This is new text‘)

This will replace the html content of all the matched elements.

<div class="AClass"><b>This is new text</b></div>
<div class="AClass"><b>This is new text</b></div>

  

jQuery html() example

<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
 
<style type="text/css">
	.htmlClass{
		padding:8px;
		border:1px solid blue;
		margin-bottom:8px;
	}
</style>
 
</head>
<body>
  <h1>jQuery html() example</h1>
 
  <div class="htmlClass">I‘m going to replate by something ....</div>
 
  <div class="htmlClass">I‘m going to replate by something 2....</div>
 
  <p>
  <button id="getHtml">html()</button>
  <button id="setHtml">html(‘xxx‘)</button>
  <button id="reset">reset</button>
  </p>
 
  <script type="text/javascript">
 
    $("#getHtml").click(function () {
 
	  alert($(‘.htmlClass‘).html());
 
    });
 
    $("#setHtml").click(function () {
 
	  $(‘.htmlClass‘).html(‘<b>This is a new text</b>‘);
 
    });
 
    $("#reset").click(function () {
	  location.reload();
    });
 
</script>
</body>
</html>

  

jQuery html() example

原文:http://www.cnblogs.com/hephec/p/4574439.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!