如何在span类中删除文本字符串的一部分 [英] How to remove a a part of a text string in a span class
本文介绍了如何在span类中删除文本字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在span类中有两个文本字符串,我需要使用jQuery或vanilla Javascript删除其中的一部分.字符串是#AAA和#AAB
I have a two text strings in a span class and I need to remove a part of it with either jQuery or vanilla Javascript. The string is #AAA and #AAB
为了达到这个结果,我使用了一些变体,但是在这个span类上,它似乎不起作用.
I have used a few variations before for achieving this result, but on this span class it doesn´t seem to work.
这是我尝试过的脚本:
$j('document').ready(
function() {
$j("span.sku").text(function() {
return $(this).text().replace(/(#AAA |#AAB)/g, "");
}).addClass("remove");
});
和
$j('body').ready(function(){
$j( "span.sku:contains('#AAA')" ).addClass( "remove" );
$j( "span.sku:contains('#AAB')" ).addClass( "remove" );
});
最后一个我试图设置一个我可以用CSS隐藏的类.
The last one I tried to just set a class that I could hide with CSS.
$ j用于站点正在使用的非冲突模式.
The $j is for non-conflict mode that the site is using.
html:
<div class="product_meta">
<span class="sku_wrapper">SKU: <span class="sku" itemprop="sku">K1J2AEA#AAA</span></span>
<meta itemprop="brand" content="HP">
<meta itemprop="gtin14" content="">
<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition">
<span class="posted_in">Categorie: <a href="/category/computers/notebooks/" rel="tag">Notebooks</a></span>
</div>
推荐答案
对于字符串文字,您需要使用'
或"
,还需要删除正则表达式中的(space)
You need to use '
or "
for string literals also need to remove the (space ) in the regex
var $j = jQuery.noConflict(true);
$j(function($) { //dom ready handler receives a jQuery object reference as its parameter so you can assign it ti $ and use it inside the handler
$("span.sku").text(function(i, text) {
return text.replace(/#AAA|#AAB/g, "");
}).addClass('remove');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="product_meta">
<span class="sku_wrapper">SKU: <span class="sku" itemprop="sku">K1J2AEA#AAA</span></span>
<meta itemprop="brand" content="HP">
<meta itemprop="gtin14" content="">
<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition">
<span class="posted_in">Categorie: <a href="/category/computers/notebooks/" rel="tag">Notebooks</a></span>
</div>
这篇关于如何在span类中删除文本字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文