2015-08-28 112 views

回答

2

您可以使用CSS visibility屬性:

$('.class').css('visibility', 'hidden'); 
$('.class').children().css('visibility', 'visible'); 

Here is a working codepen

然後你可以通過做再次使其可見:

$('.class').css('visibility', 'visible'); 
+0

請注意,這種方式不會從頁面流中刪除元素,而只是使其不可見。 – Ahmad

2

只要你可以複製span元素的p標籤附近,然後隱藏p標籤。試試這個:

$("p").after($("p span")) 
$("p").hide() 
+2

這是非常錯誤的。這修改了DOM結構。這也重複了內容。 http://jsfiddle.net/rjnw6d15/ – undefined

+0

這使DOM中的變化,但不是在標記。如果你小心你的工作,那就沒有問題。這是可以做到這一點的唯一方式,不需要使用隱形技巧。 – Ahmad

+0

他/她應該小心嗎?什麼是「隱形技巧」?不,這不是唯一的解決方案,實際上你的代碼甚至不是一個解決方案,因爲如果有多個'p'元素,它不能解決問題。 – undefined

0

你可以不隱藏文本節點。您只能刪除它們或用另一個元素包裝它們並隱藏該包裝器元素。

以下代碼片段隱藏了非span元素並刪除了文本節點子項。

$('p.class').contents().each(function() { 
    var $this = $(this); 
    if (this.nodeType === 3 && $.trim(this.nodeValue)) { 
     $this.remove(); 
     // in case that you just want to hide the text node 
     // $this.wrap('<i>').parent().hide(); 
    } else if (this.nodeType === 1 && this.tagName !== 'SPAN') { 
     $this.hide(); 
    } 
}); 

http://jsfiddle.net/xxpopvje/

0

使用此,並嘗試自動換行是隱藏在包裝p元素像

<p><span>Text not to hide</span><br/><p>Text to hide</p></p> 

$('p').find('*').not('span').css('display','none'); 
相關問題