简体   繁体   English

修剪CSS类的第一个和最后一个字符

[英]Trim first and last character from CSS class

I have a css class .clsCatOffCount that happens multiple times on a site. 我有一个CSS类.clsCatOffCount ,该类在站点上发生多次。 It is generated as a number with parenthesis around it. 它作为带有括号的数字生成。

example: (20) 范例:(20)

I am trying to remove the parenthesis from the generated number. 我正在尝试从生成的数字中删除括号。

I am using the below however it removes the parenthesis, but it changes all the values to the same number after removing the parenthesis. 我正在使用以下内容,但是它删除了括号,但是在删除括号后,它将所有值更改为相同的数字。

<script language="JavaScript" type="text/javascript">
$('span.clsCatOffCount').html($('span.clsCatOffCount').html().replace('(',''))
$('span.clsCatOffCount').html($('span.clsCatOffCount').html().replace(')',''))
</script>

Here is the html generated and there is another class I may be able to use that is unique clsCatTree[id^="CatImg"] . 这是生成的html,还有另一个我可以使用的类,它是唯一的clsCatTree[id^="CatImg"]

<div id="SbCatMenu" class="__web-inspector-hide-shortcut__">
<dl id="dlCatLvl1" class="clsCatLvl1 clsOffCat1">
<dd class="clsCatTree1 clsCTree1" id="CatImg1"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=1">Backdrop<span class="clsCatOffCount"> (2)</span></a></dd>
<dd class="clsCatTree1 clsCTree1" id="CatImg2"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=2">Banner Stands<span class="clsCatOffCount"> (12)</span></a></dd>
<dd class="clsCatTree1 clsCTree1" id="CatImg3"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=3">Data Sheet<span class="clsCatOffCount"> (16)</span></a></dd>
<dd class="clsCatTree1 clsCTree1" id="CatImg4"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=4">Giveaways<span class="clsCatOffCount"> (9)</span></a></dd>
<dd class="clsCatTree1 clsCTree1" id="CatImg5"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=5">Table Top<span class="clsCatOffCount"> (0)</span></a></dd>
</dl>

</div>

I would do a loop... 我会做一个循环...

$('span.clsCatOffCount').each(function () {
    $(this).html($(this).html().replace('(', '').replace(')', ''));
});

It's the same because you're using the html of the first element with that class, you should loop throught them and replace like : 这是相同的,因为您正在使用该类的第一个元素的html,因此应遍历它们并像下面这样替换:

 $('span.clsCatOffCount').each((i,e) => { var newHtml = $(e).html().replace(/\\((\\w+)\\)/g, '$1'); $(e).html(newHtml) }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="SbCatMenu" class="__web-inspector-hide-shortcut__"> <dl id="dlCatLvl1" class="clsCatLvl1 clsOffCat1"> <dd class="clsCatTree1 clsCTree1" id="CatImg1"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=1">Backdrop<span class="clsCatOffCount"> (2)</span></a></dd> <dd class="clsCatTree1 clsCTree1" id="CatImg2"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=2">Banner Stands<span class="clsCatOffCount"> (12)</span></a></dd> <dd class="clsCatTree1 clsCTree1" id="CatImg3"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=3">Data Sheet<span class="clsCatOffCount"> (16)</span></a></dd> <dd class="clsCatTree1 clsCTree1" id="CatImg4"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=4">Giveaways<span class="clsCatOffCount"> (9)</span></a></dd> <dd class="clsCatTree1 clsCTree1" id="CatImg5"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=5">Table Top<span class="clsCatOffCount"> (0)</span></a></dd> </dl> </div> 

The issue with your code is that using .html() to read an element's HTML will evaluate only the first element in the selection, which in your case is (2) . 您的代码的问题在于,使用.html() 读取元素的HTML将评估选择中的第一个元素 ,在您的情况下为(2)

However, using .html("hello world") to set an element's HTML will updated all elements in the selection. 但是,使用.html("hello world") 设置元素的HTML将更新所选内容中的所有元素

Therefore, you're setting all elements to the .html() of the first. 因此,您要将所有元素都设置为第一个元素的.html()

Instead, pass a function to the jQuery .html() method to evaluate them each on a case-by-case basis: 而是将一个函数传递给jQuery .html()方法,以根据具体情况对它们进行评估:

 $(".clsCatOffCount").html(function() { return $(this).html().replace("(", "").replace(")", ""); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="clsCatOffCount">(1)</span> <span class="clsCatOffCount">(2)</span> <span class="clsCatOffCount">(3)</span> <span class="clsCatOffCount">(4)</span> <span class="clsCatOffCount">(5)</span> 

const countOffs = $('span.clsCatOffCount');
countOffs.each((idx, el) => {
  const content = el.innerHTML;
  el.innerHTML = content.replace(/\(|\)/g, '')
});

Or just use Tyler's answer with the regex replace above 或者只是使用泰勒的答案与上面的正则表达式替换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM