jQuery文本值相关操作

html:给div设置值和获取值,会解析html标签

text:给div设置值和获取值,不会解析html标签

val:获取input里的值

代码如下

<html>

<head>
<title></title>
</head>
    <style>
        div{
            width:100px;
            height:100px;
            background:red;
            margin-bottom:40px;
        }
        
    </style>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
        
        $(function(){
            $('.cHtml').html('<p>我是学生</p>')  //会把p标签给解析出来,然后我是学生内容添加到div上
            console.log($('.cHtml').html())   //$('.cHtml').html()取出div里的内容
      $('.cText').text('<p>我是学生</p>')   //不会解析标签,会把标签直接添加到div上
      console.log($(
'.cText').text()) //$('.cText').text()取出div里的内容
      $(
'input').val('124') //给input框里设置值
      console.log($(
'input').val()) })   //取出input框的值

  </script>
<body>
  <div class="cHtml"></div>
   <div class="cText"></div>
  <input />
</body>
</html>

效果图

 

转载于:https://www.cnblogs.com/superCwen/p/9886408.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值