2011-09-02 39 views
2

我以前发过类似的问题,但这有点不同。我正在寻找使用jQuery从下面的价格代码中删除冒号。通过jQuery从部分文本中删除冒号?

<font class="pricecolor colors_productprice"> 
     <div class="dealtext"></div> 
     : $58.05 
</font> 

到目前为止,我认为可能是有些实现这样的,我只是需要另一套眼睛加以纠正:

$('.pricecolor:contains(" : ")').remove(colon??); 

它仍然看起来不正确,也许我需要一个变种设置为get()

回答

2
$('*').each(function() { 
    $(this).html($(this).html().replace(":", "")); 
}); 
+0

这样做对我来说,谢谢大家! – ToddN

+0

适用于OP的例子,但不是很健壮。这简单地跳过它看到的第一个冒号 - http://jsfiddle.net/veVyY/。哎哟。 –

+0

好点肖恩,幸运的是我只需要打一个冒号。因此,为了将来的参考,人们可能必须修改多个清除。 – ToddN

0
function findAndReplace(elements, textToFind, textToPlace) { 
      $.each(elements.contents(), function() { 
       // This is added to make sure the nodes you request are text nodes 
       if (this.nodeType == 3) 
        this.nodeValue = this.nodeValue.replace(textToFind, textToPlace); 
      }); 

     } 

     findAndReplace($('div.dealtext'), ':'); 
+1

这似乎是一个非常沉重的*** ***的方式来做到这一点..定期表达式将是一个更好的解决方案。请记住'$ .each'运行非常缓慢。此外,您正在进行大量的DOM调用,这也会导致浏览器崩溃。 – rlemon

+0

我投票了你的评论,因为它是有效的,但我的方法工作,以取代整个DOM中的所有冒号。在这种情况下,我的解决方案显然不是最好的选择,因为OP只想删除一个特定的冒号,但是在某些情况下我的代码可能是必需的。 – kramden88

+0

我只评论你的利益。我看到很多人过度依赖JQuery来完成传统(无框架)JavaScript所能做的简单事情。不是说你的代码没有用,我确定它是。只是指出一些性能可能受到影响的领域。观看此视频http://www.youtube.com/watch?v=mHtdZgou0qU&feature=channel_video_title – rlemon

0

我认为你可以这样做以下。

$(".pricecolor colors_productprice").html($(".pricecolor colors_productprice").html()replace(/:/g, "")) 

希望这有助于!

0
var e = $(".pricecolor:contains(':')"); 
e.text(e.text().replace(/\s*:\s*/, '')); 
0

这将工作:

$(".pricecolor:contains(' : ')").each(function(){ 
    $(this).html($(this).html().replace(" : ", "")); 
}); 
0

给这一个镜头:

$('.pricecolor:contains(" : ")').text(function(index, text) { 
    return text.replace(/:/g, ""); 
}); 

我认为你需要传递的,而不是一个字符串$的.text(函数)这样的以避免损坏使用'.pricecolor:contains(“:”)'选择器找到的其他内容。

1

我发现这个问题,因为我需要它,我最终使用该功能做同样的事情......

$('.pricecolor:contains(" : ")').html(function(index,oldhtml) { 
    return oldhtml.replace(' : ',''); 
});