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

$.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);

猜你喜欢

转载自www.cnblogs.com/sandraryan/p/11528448.html