隐藏在页面上重复多次的','逗号

时间:2018-10-14 20:29:13

标签: javascript jquery css replace

我试图隐藏在CMS生成的页面上多次出现的','(逗号)。在此处查看页面:

http://www.ucd.ie/earth/sandbox/test/01.html

我在标记之前尝试了以下代码(未成功):

<script language="javascript">
var container = $(".hide-comma");
var result = container.html().replace(/,/g, "");
container.html(result);
</script> 

2 个答案:

答案 0 :(得分:1)

我认为您想替换div中的所有“,”。因此您可以这样做。.在div中获取html,然后替换所有的','

function replaceComma(){

  $('#divWithComma').html($('#divWithComma').html().replace(",",""));
  console.log('replaced the comma, if you want to see hiding the comma please run the snippet again');
}

function hideComma(){
  
  let text = $('#divWithComma').html();
  let stringArray = text.split(',');
  let finalHtml = "";
  
  for(let i = 0; i<stringArray.length-1; i++){
    finalHtml += stringArray[i] + '<span style="display:none;aria-hidden=true">,</span><p></p>';
  }
  
  $('#divWithComma').html(finalHtml);
  
  console.log("yes hide the comma");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='divWithComma'>
  <p>,</p>
  <input type='button' onclick='replaceComma()' value='replace comma' />
  <input type='button' onclick='hideComma()' value='hide comma' />
</div>

答案 1 :(得分:1)

在加载页面完成后输入以下代码:

 $('.team-description').each(function(i , v){
     var t = $(v).html().replace(/,/g, '');
     $(v).html(t);
 });

,请参阅下面的链接 https://jsfiddle.net/sajjadgol/xpvt214o/881975/