Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从内容中查找文本,然后隐藏_Javascript_Jquery_Html_Regex_Text - Fatal编程技术网

Javascript 从内容中查找文本,然后隐藏

Javascript 从内容中查找文本,然后隐藏,javascript,jquery,html,regex,text,Javascript,Jquery,Html,Regex,Text,我试图通过jquery从内容中隐藏特定的文本元素 HTML: 我想要这个: <div id="name"> Trying hide <p style="display: none;">specific</p> Text elements from content. </div> 或者jquery提供的任何其他简单解决方案 像这样简单的事情 var nameText = $('#name').text(); var newText = nameTe

我试图通过jquery从内容中隐藏特定的文本元素

HTML:

我想要这个:

<div id="name">
Trying hide <p style="display: none;">specific</p> Text elements from content.
</div>
或者jquery提供的任何其他简单解决方案

像这样简单的事情

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

将id添加到p标记中,您可以通过jquery将其隐藏,如

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

我为你做一个函数

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选择器将第一个参数放在元素上,并将想要终止的字符串放在第二个参数上


对于您来说,您面临的问题是什么?
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');