2016-09-29 33 views
-4

我正在試圖隱藏specific來自jquery內容的文本元素。從內容中查找文本然後隱藏

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其他任何簡單的解決辦法?

+0
+0

錯字錯,現在編輯 – Aariba

+0

什麼問題你面對? –

回答

2

財產以後簡單的像

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

添加ID到您的p標籤,你可以通過jQuery的像

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

我做功能爲你

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'); 

地方1RST隱藏使用jquery選擇器和第二個參數來配置你的elem字符串想要殺死

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