如何在jquery中使用substr()函数?

Val*_* Do 33 jquery

如何在我需要的脚本中使用substr函数 substr(0,25);

<a class="dep_buttons" href="#"> something text something text something text something text something text something text </a>

$('.dep_buttons').mouseover(function(){
    if($(this).text().length > 30) {
        $(this).stop().animate({height:"150px"},150);
    }
    $(".dep_buttons").mouseout(function(){
        $(this).stop().animate({height:"40px"},150);
    });
});
Run Code Online (Sandbox Code Playgroud)

Moo*_* GK 61

从字符串中提取字符:

var str = "Hello world!";
var res = str.substring(1,4);
Run Code Online (Sandbox Code Playgroud)

结果res将是:

ell
Run Code Online (Sandbox Code Playgroud)

http://www.w3schools.com/jsref/jsref_substring.asp

$('.dep_buttons').mouseover(function(){
    $(this).text().substring(0,25);
    if($(this).text().length > 30) {
        $(this).stop().animate({height:"150px"},150);
    }
    $(".dep_buttons").mouseout(function(){
        $(this).stop().animate({height:"40px"},150);
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 请不要参考W3Schools(参见:http://www.w3fools.com/).请改用MDN. (3认同)

Pra*_*een 6

如果你想从a标签中提取出来

$('.dep_buttons').text().substr(0,25)
Run Code Online (Sandbox Code Playgroud)

使用鼠标悬停事件,

$(this).text($(this).text().substr(0, 25));
Run Code Online (Sandbox Code Playgroud)

以上将提取标签的文本,然后再次提取将其分配回来.