本文介绍了jQuery从字符串中删除'-'字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串"-123445".是否可以从字符串中删除-"字符?

I have a string "-123445". Is it possible to remove the '-' character from the string?

我尝试了以下操作,但无济于事:

I have tried the following but to no avail:

$mylabel.text("-123456");
$mylabel.text().replace('-', '');

推荐答案

$mylabel.text( $mylabel.text().replace('-', '') );

由于text()获取值,并且text( "someValue" )设置了值,所以您只需将一个放在另一个内部即可.

Since text() gets the value, and text( "someValue" ) sets the value, you just place one inside the other.

等同于做

var newValue = $mylabel.text().replace('-', '');
$mylabel.text( newValue );


我希望我正确理解了这个问题.我假设$mylabel在jQuery对象中引用DOM元素,并且字符串在该元素的内容中.

I hope I understood the question correctly. I'm assuming $mylabel is referencing a DOM element in a jQuery object, and the string is in the content of the element.

如果字符串位于DOM以外的其他变量中,那么您可能想在将该变量插入之前针对该变量调用.replace()函数.

If the string is in some other variable not part of the DOM, then you would likely want to call the .replace() function against that variable before you insert it into the DOM.

赞:

var someVariable = "-123456";
$mylabel.text( someVariable.replace('-', '') );

或更详细的版本:

var someVariable = "-123456";
someVariable = someVariable.replace('-', '');
$mylabel.text( someVariable );

这篇关于jQuery从字符串中删除'-'字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 23:55
查看更多