详解jQuery设置内容和属性

2022-10-20,,,,

一、设置内容以及回调函数

方法

  1. text() - 设置或返回所选元素的文本内容
  2. html() - 设置或返回所选元素的内容(包括 html 标记)
  3. val() - 设置或返回表单字段的值

例子

$("#btn1").click(function(){
 $("#test1").text("hello world!");
});//设置文本内容
$("#btn2").click(function(){
 $("#test2").html("<b>hello world!</b>");
});//设置元素内容
$("#btn3").click(function(){
 $("#test3").val("dolly duck");
});//设置值
$("#btn1").click(function(){
 $("#test1").text(function(i,origtext){
  return "old text: " + origtext + " new text: hello world!
  (index: " + i + ")";
 });
});//返回文本内容

$("#btn2").click(function(){
 $("#test2").html(function(i,origtext){
  return "old html: " + origtext + " new html: hello <b>world!</b>
  (index: " + i + ")";
 });
});//返回元素内容

tips

回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。

二、设置属性以及回调函数

方法

attr() - 设置属性值

例子

$("button").click(function(){
 $("#w3s").attr("href","http://www.w3school.com.cn/jquery");
});//设置单个属性

$("button").click(function(){
 $("#w3s").attr({
  "href" : "http://www.w3school.com.cn/jquery",
  "title" : "w3school jquery tutorial"
 });
});//设置多个属性
$("button").click(function(){
 $("#w3s").attr("href", function(i,origvalue){
  return origvalue + "/jquery";
 });
});//回调函数

总代码

<!doctype html>
	<head>
	<title>jq设置内容和属性</title>
	<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){
			$("#bt1").click(function(){
				$("#p1").text("hello world1!");
			});
			$("#bt2").click(function(){
				$("#p2").html("<h1>hello world2!</h1>");
			});
			$("#bt3").click(function(){
				$("#t1").val("hello world3!");
			});
			$("#bt4").click(function(){
				$("#p1").text(function(i,origtext){
					return "旧文本:"+origtext+"新文本:这是新文本(index:"+i+")"
				});
			});
			$("#bt5").click(function(){
				$("#p2").html(function(i,origtext){
					return "旧html:"+origtext+"新html:<b>这是新html</b>(index:"+i+")"
				});
			});
			$("#bt6").click(function(){
				$("#t1").val(function(i,origtext){
					return "旧值:"+origtext+"新值:这是新值(index:"+i+")"
				});
			});
			$("#bt7").click(function(){
				$("a").attr("href","https://tieba.baidu.com");
			});
			$("#bt8").click(function(){
				$("a").attr("href",function(i,origvalue){return origvalue + "/s?wd=1&rsv_spt=1&rsv_iqid=0xbc3b96600002e5f4&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=2&rsv_sug1=1&rsv_sug7=100&rsv_sug2=0&inputt=163&rsv_sug4=1686"});
			});
		});
	</script>
	</head>

	<body>
		<input type="button" id="bt1" value="设置文本" "">
		<input type="button" id="bt2" value="设置html" "">
		<input type="button" id="bt3" value="设置值" "">
		<p id="p1">这是文本</p>
		<p id="p2">这是html</p>
		<p>这是:<input type="text" value="值" id="t1" style ="width:500px"></p>
		<input type="button" id="bt4" value="显示旧/新文本" "">
		<input type="button" id="bt5" value="显示旧/新html" "">
		<input type="button" id="bt6" value="显示旧/新值" "">
		<hr><!--以上是设置内容,一下是设置属性-->
		<input type="button" id="bt7" value="改变href值为贴吧" "">
		<p><a id="baidu" href="https://www.baidu.com" rel="external nofollow" target="_blank">百度</a></p>
		<input type="button" id="bt8" value="改变href值为搜索1(回调函数)" "">
	</body>
</html>

以上所述是小编给大家介绍的jquery设置内容和属性详解整合,希望对大家有所帮助

《详解jQuery设置内容和属性.doc》

下载本文的Word格式文档,以方便收藏与打印。