I have such a text. In p tag I need two symbols from the end of the text to be deleted. How can I do it through javascript?
我有这样的文字。在p标签中,我需要从文本末尾删除两个符号。我怎么能通过javascript做到这一点?
<p>text</p>
2 个解决方案
#1
You're looking for .slice
:
你在找.slice:
<p>text</p>
<script>
var p = document.getElementsByTagName('p')[0];
p.innerHTML = p.innerHTML.slice(0, -2); // returns "te"
</script>
#2
try this
html
<p id="txt">text+-</p>
<input type="button" id="btn" value="click">
js
$(function(){
$('#btn').click(function(){
var text = $('#txt').html();
var newStr = text.slice(0,-2);
alert(newStr);
});
});
this will do the trick for you. It uses jquery and javascript. Check out the fiddle eliminate last two symbols from a p element
这将为你做到这一点。它使用jquery和javascript。检查小提琴消除p元素中的最后两个符号
#1
You're looking for .slice
:
你在找.slice:
<p>text</p>
<script>
var p = document.getElementsByTagName('p')[0];
p.innerHTML = p.innerHTML.slice(0, -2); // returns "te"
</script>
#2
try this
html
<p id="txt">text+-</p>
<input type="button" id="btn" value="click">
js
$(function(){
$('#btn').click(function(){
var text = $('#txt').html();
var newStr = text.slice(0,-2);
alert(newStr);
});
});
this will do the trick for you. It uses jquery and javascript. Check out the fiddle eliminate last two symbols from a p element
这将为你做到这一点。它使用jquery和javascript。检查小提琴消除p元素中的最后两个符号