jQuery修改标签的内容或值

我一直知道要修改标签的内容和值有三种方式,但是最近遇到一项目却让我在改变表单元素的值上浪费了很多时间,其实我大概都知道他们的区别在哪里,只是有的时候总是会忘记,现在就来给几个简单的例子,希望以后不要再犯糊涂;

1.关于表单

这里我就写一个关于textarea的例子吧,

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<style type="text/css">
.modle-wrap{
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 999;
    left: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5
    )!important;
    background: #000;
    filter: alpha(opacity=80); 	/*ie6/7/8*/
    display: none;
}
.modle-container{
    width: 100%;
    height: 100%;
    position: relative;
    z-index: 2;
}
.refuse{
	width: 740px;
	height: 350px;
	position: absolute;
	left:50%;
	top:50%;
	margin-top:-175px;
	margin-left:-370px;
	background: #fff;
}
.refuse_title{
	height: 50px;
	font-size: 16px;
	line-height: 50px;
	background: #1A8CDC;
	color: #fff;
}
.refuse_content{
	padding:40px 30px 0;
	width: 100%;
}
.refuse_word_height{
	width: 628px;
	height: 150px;
	overflow:scroll;
	overflow-x: hidden;
}
.refuse_word{
	width: 628px;
	border:1px solid #CCCCCC;
	resize: none;
	min-height: 150px;
	height: auto;
	color: #999;
	line-height: 30px;
	padding:0 5px;
}
.refuse_btn{
	width: 160px;
	line-height: 36px;
	height: 36px;
	text-align: center;
	border:1px solid #1A8CDC;
	background: #fff;
	margin-top:40px;
	border-radius: 2px;
}
.refuse_btn+.refuse_btn{
	margin-left: 40px;
}
.refuse_btn.on{
	background: #1A8CDC;
	color: #fff;
}

</style>
<body>
<p class="agreen text_center">
	<button class="pc_btn on pointer pc_yes">同意</button>
	<button class="pc_btn pointer pc_no" id="">拒绝</button>
</p>
<div class="modle-wrap refuse_box">
	<div class="modle-container">
		<div class="refuse">
			<h5 class="refuse_title">拒绝原因</h5>
			<div class="refuse_content">
				<span class="float_left">原因</span>
				<div class="float_right" id="refuse_word_height">
					<textarea class="refuse_word" id="refuse_word">拒绝原绝原因拒绝原因拒绝原因拒绝原因拒绝原因拒绝原因拒绝原因拒绝原因拒绝原因拒绝原</textarea>
				</div>
			</div>
			<p class="refuse_btnOut">
				<button class="on refuse_btn yes" id="yes">确定</button>
				<button class="refuse_btn close" id="close">关闭</button>
			</p>
		</div>
	</div>
</div>
<script type="text/javascript" src="jquery-3.0.0.min.js"></script>
<script type="text/javascript">
	$(function(){
		$(".pc_no").click(function(){
				$(".refuse_box").show();
		})

	  	var refuse_word=$("#refuse_word").text();
	  	var focus=0;
	      $("#refuse_word").focus(function(){
	          focus+=1;
	          console.log(focus);
	          if(focus==1){
	              $("#refuse_word").text("");
	          }
	      });
	      $(".close").click(function(){
	          $(".modle-wrap").hide();
	      })
	        $(".yes").click(function () {
	          focus = 0;
	          $(".refuse_box").hide();
	          refuse_word="请输入拒绝原因";
	          console.log(refuse_word);
	          $("#refuse_word").val(refuse_word);
	      })
	     $(".refuse_btnOut .refuse_btn").click(function(){
			$(this).addClass("on").siblings('.refuse_btn').removeClass('on');
		})
	})
</script>
</body>
</html>
描述一下这个例子吧,点击“拒绝”会弹出拒绝原因填写页面,让文本框第一次获取焦点清空文本,在未提交(未按确定)之前关闭此页再次用过“拒绝”按钮打开此页任然保留数据,当点击“确认”就会将框类的值跟换成“请输入拒绝原因”;

我之前用text()来获取值改更改值,发现始终在页面显示不出来,只能打印出来

总结,表单元素顺利修改他的内容,就的使用val();

2.再说说html()与text()的区别

不同点一:

   html()在获取元素内容时,如果选择器匹配多于一个的元素,那么只有第一个匹配元素的 HTML 内容会被获取。

   text()在获取元素内容时,结果是由所有匹配元素包含的文本内容组合起来的文本

  1. <p>段落一</p>  
  2. <p>段落二<p>   
  3.   
  4.   
  5. $(function(){  
  6.  alert($("p").text());  
  7. })  
  8.   
  9. 弹出框结果为: 段落一段落二  

 

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $(function(){  
  2.     alert($("p").html());  
  3. })  

 不同点二:

html()在获取内容时,会将其中的其他标签也读取出来

  text()在获取内容时,会忽略其中的标签

不同点三:

    html()在更改内容时,可以加入标签

    text()更改的全是内容



  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值