【问题标题】:Find Text from content then hide从内容中查找文本然后隐藏
【发布时间】:2016-09-29 13:01:17
【问题描述】:

我正在尝试通过 jquery 从内容中隐藏 specific 文本元素。

HTML:

<div id="name">
Trying hide specific Text elements from content.
</div>

我想要这个:

<div id="name">
Trying hide <p style="display: none;">specific</p> Text elements from content.
</div>

或者其他任何 jquery 的简单解决方案?

【问题讨论】:

  • 错字,现已编辑,
  • 您遇到了什么问题?

标签: javascript jquery html regex text


【解决方案1】:

简单的事情

var nameText = $('#name').text();
var newText = nameText.replace('specific','<p style="display:none">specific</p>');
$('#name').html(newText) // use html() here, not text()

【讨论】:

    【解决方案2】:

    在你的p标签中添加一个id,你可以通过jquery来隐藏它

    $( document ).ready(function() {
        $("#p-id").hide();
    });
    

    【讨论】:

      【解决方案3】:

      我为你做功能

      function hideIt(elem, exclude){
        var text = elem[0].innerText;
        var exculude = exclude;
        var match = text.match(exclude);
      
        if(match === null){
          console.log('no match founded')
        }
        else{
          for(var i =0 ; i<match.length; i++){
              elem[0].innerText = text.replace(match[i],"")
          }
        }
      
      }
      hideIt($('#name'),'specific');
      

      用 jquery 选择器放置第一个参数你的元素,第二个参数是要杀死的字符串

      https://jsfiddle.net/xps4553m/5/给你

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 2015-04-25
        相关资源
        最近更新 更多