【问题标题】:Hide a ',' comma that repeats multiple times on a page隐藏在页面上重复多次的“,”逗号
【发布时间】:2019-03-19 07:07:06
【问题描述】:

我正在尝试隐藏在 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> 

【问题讨论】:

  • hide-comma div 需要隐藏逗号吗?
  • HTML 中的精确 是您要删除的逗号?在该示例中,ID 为 .hide-commaany DIV 中没有没有内容。您的代码可能会或可能不会正常工作,但应该从修改实际存在的内容开始?我错过了什么? (我敢打赌,你的意思是.team-info DIV)

标签: javascript jquery css replace


【解决方案1】:

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

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

并查看以下链接 https://jsfiddle.net/sajjadgol/xpvt214o/881975/

【讨论】:

    【解决方案2】:

    我认为您想替换 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>

    【讨论】:

    • 请加aria-hidden="true" next tho style="display:none",可访问性很容易实现,可以免费帮助很多人。
    • 非常感谢您的代码。最后,我使用了给出的较短的选项。非常感谢您抽出宝贵的时间来写作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多