jQuery的HTML操作

本文通过jQuery脚本展示了如何使用$.html(), $.append(), $.prepend(), $.after()和$.before()方法来修改HTML内容,包括文本替换、元素追加、移位等操作,并提供了具体的代码示例。

$(selector).html(content);   修改HTML内容。

$(selector).append(content);    将内容添加所有匹配元素的后面。

$(selector).prepend(content);    将内容添加所有匹配元素的前面。

$(selector).after(content);    将内容添加所有匹配元素的下一行。

$(selector).before(content);    将内容添加所有匹配元素的前一行。


示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script type="text/javascript" src="jquery-1.6.4.js"></script>
  <script type="text/javascript">
	$(document).ready(function(){
		$("button").click(function(){
			$("#p1").html("This is another content.");
			$("#p2").append("This is another content.");
			$("#p3").prepend("This is another content.");
			$("#p4").after("This is another content.");
			$("#p5").before("This is another content.");
		});
	});
  </script>
 </HEAD>

 <BODY>
  <button type="button">Change</button>
  <p id="p1">This is a paragraph with little content.</p>
  <p id="p2">This is a paragraph with little content.</p>
  <p id="p3">This is a paragraph with little content.</p>
  <p id="p4">This is a paragraph with little content.</p>
  <p id="p5">This is a paragraph with little content.</p>
 </BODY>
</HTML>


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值