使用jQuery删除逗号后的所有字符
我想使用jQuery,通过将元素的类作为目标,删除字符串中“,”之后的所有字符。我发现的其他解决方案是使用javascript,以字符串本身而不是类为目标 我的代码是:使用jQuery删除逗号后的所有字符,jquery,Jquery,我想使用jQuery,通过将元素的类作为目标,删除字符串中“,”之后的所有字符。我发现的其他解决方案是使用javascript,以字符串本身而不是类为目标 我的代码是: <td class="priceinfo">€779,34 </td> 779,34欧元 如何删除td元素文本中“,”之后的字符?您可以尝试以下操作: str = str.substr(0, str.lastIndexOf(",")); 您可以尝试以下方法: str = str.s
<td class="priceinfo">€779,34 </td>
779,34欧元
如何删除td元素文本中“,”之后的字符?您可以尝试以下操作:
str = str.substr(0, str.lastIndexOf(","));
您可以尝试以下方法:
str = str.substr(0, str.lastIndexOf(","));
您可以使用jQuery的
text()
方法来实现这一点。通过提供函数作为方法调用的参数,将对集合中的每个元素执行操作
$('.priceinfo').text(函数(i,t){
返回t.substr(0,t.lastIndexOf(',');
});代码>
€779,34
€2,34
€1,290,34
您可以使用jQuery的text()
方法来实现这一点。通过提供函数作为方法调用的参数,将对集合中的每个元素执行操作
$('.priceinfo').text(函数(i,t){
返回t.substr(0,t.lastIndexOf(',');
});代码>
€779,34
€2,34
€1,290,34
这样做可以删除第一个逗号后的所有字符:
$(function(){
$('.priceinfo').each(function(){
text = $(this).text();
textSplit = text.split(',');
$(this).text(textSplit[0]);
});
});
这样做可以删除第一个逗号后的所有字符:
$(function(){
$('.priceinfo').each(function(){
text = $(this).text();
textSplit = text.split(',');
$(this).text(textSplit[0]);
});
});
或您不必使用坦克锤钉子,只需使用jQuery free即可™ 解决方案:
[].forEach.call(document.querySelectorAll('.priceinfo'), function (el) {
var h = el.innerHTML;
el.innerHTML = h.substr(0, h.indexOf(','));
});
或者您不必使用坦克来敲打钉子,只需使用jQuery free即可™ 解决方案:
[].forEach.call(document.querySelectorAll('.priceinfo'), function (el) {
var h = el.innerHTML;
el.innerHTML = h.substr(0, h.indexOf(','));
});
$('.priceinfo').text((i,t)=>t.replace(/,.*/,'')代码>$('.priceinfo')。文本((i,t)=>t.replace(/,.*/,'')
只是想知道为什么要传递两个参数?@ShibinRagh:第一个用于索引,第二个用于当前值。这就是jquery中回调函数的语法工作原理。这些参数由jquery定义i
是匹配集合中元素的索引,t
是它的当前文本值。有关更多信息,请参阅。谢谢,了解的人必须知道为什么要传递两个参数?@ShibinRagh:第一个用于索引,第二个用于当前值。这就是回调函数在jquery中的语法工作方式。这些参数由jquery定义i
是匹配集合中元素的索引,t
是它的当前文本值。了解更多信息,谢谢,明白了吗