首页 > Web开发 > 详细

jq杂项方法/工具方法----trim() html() val() text() attr()

时间:2019-09-16 18:21:22      阅读:66      评论:0      收藏:0      [点我收藏+]

$.trim() 函数用于去除字符串两端的空白字符。在中间的时候不会去掉。

var str = ‘        去除字符串左右两端的空格,换行   ,制表符。      ‘;
res = $ .trim(str);
console.log(str);
console.log(res);

技术分享图片

html(); //设置或获取元素的html内容
text();  //text内容
val();   //表单value值
attr();   //属性值(html提供的默认值和自定义值)
<div class="wrap">this is a wrapppppp
        <p>para</p>
        <mark>mark text</mark>
        <h2>header 2</h2>
    </div>


var wrap = $(‘.wrap‘);
    var res = wrap.html();
    console.log(res);

    res = wrap.text();
    console.log(res);

技术分享图片

 

 

wrap.text(‘hhhhhhhhhh‘);
会覆盖掉页面内容。
  <input class="inp" type="text" value="the value">

  var inp = $(‘.inp‘);
  var res = inp.val();
  console.log(res);
显示为:
技术分享图片

 技术分享图片

 

 attr

 

var h2 = $(‘.wrap h2‘);
    var res = h2.attr(‘class‘);
    console.log(res);
    
    h2.attr(‘class‘,‘h2‘);
    res = h2.attr(‘class‘);
    console.log(res);

    h2.attr(‘newattr‘,‘hi‘);
    res = h2.attr(‘newattr‘);
    console.log(res);

技术分享图片

 

 

jq杂项方法/工具方法----trim() html() val() text() attr()

原文:https://www.cnblogs.com/sandraryan/p/11528448.html

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