在div中检测字符并将其删除Javascript或jQuery [英] Detect character in div and remove it Javascript or jQuery
本文介绍了在div中检测字符并将其删除Javascript或jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个div:
<h1>$340</h1>
我需要检测该div内是否有美元符号,
如果是这样-将其删除
I need to detect if inside of this div there is a dollar symbol,
If so - remove it
我将再次说明: 到目前为止,我已经获得了这段代码,因此我设法检测出该符号是否在div中,但是我不知道如何删除它并更新DOM?
I'll explain again: I have got this code so far so I manage to detect if the symbol is in the div, but I can't figure how to remove it and update the DOM?
var sign = $('h1').text();
if (sign.indexOf("$") >= 0) {
//remove the sign
}
推荐答案
使用简单的正则表达式很容易
This is quite easy using a simple regex
var div = $('your selector here');
div.html(div.html().replace(/\$/g, ''));
这篇关于在div中检测字符并将其删除Javascript或jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文