如何在 jquery 中使用 substr() 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21423210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use substr() function in jquery?
提问by Val Do
how to use substr function in this script I need substr(0,25);
如何在我需要的这个脚本中使用 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);
});
});
回答by Moorthy GK
Extract characters from a string:
从字符串中提取字符:
var str = "Hello world!";
var res = str.substring(1,4);
The result of res
will be:
的结果res
是:
ell
http://www.w3schools.com/jsref/jsref_substring.asp
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);
});
});
回答by Praveen
If you want to extract from a
tag then
如果你想从a
标签中提取然后
$('.dep_buttons').text().substr(0,25)
With the mouseover event,
随着鼠标悬停事件,
$(this).text($(this).text().substr(0, 25));
The above will extract the text of a tag, then extract again assign it back.
以上将提取标签的文本,然后再次提取并将其分配回。