Javascript 从内容中查找文本,然后隐藏
我试图通过jquery从内容中隐藏特定的文本元素 HTML: 我想要这个: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
<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');