如何使用 jQuery 从链接文本中删除第一个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1684939/
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 do I remove the first character from a link's text with jQuery?
提问by AlexC
I want to remove first character from link's text with jQuery.
我想用 jQuery 从链接的文本中删除第一个字符。
<span class="test1"> +123.23 </span>
<span class="test2"> -13.23 </span>
I want to remove the "+" and "-" from with jQuery.
我想用 jQuery 删除“+”和“-”。
Output:
输出:
<span class="test1"> 123.23 </span>
<span class="test2"> 13.23 </span>
回答by ss.
var val = $("span").html();
$("span").html(val.substring(1, val.length));
回答by cletus
$("span.test1, span.test2").each(function() {
$(this).text($(this).text().replace(/[+-]/, ""));
});
回答by Chris Williams
// get the current text
text1 = $(".test1").html();
// set the text to the substring starting at the third character
$(".test1").html(text1.substring(2)); // extract to the end of the string
text2 = $(".test2").html();
$(".test2").html(" " + text2.substring(2)); // looks like you want to keep the leading space
回答by Soufiane Hassou
you can get/set the HTML using .html() and remove the first character using .substring(), I think it's pretty clear now, you just need to write a 2 (or 3) lines code.
您可以使用 .html() 获取/设置 HTML 并使用 .substring() 删除第一个字符,我认为现在很清楚,您只需要编写 2(或 3)行代码。