2014-06-12 25 views
1

我有以下HTML:jQuery的隱藏內部HTML,除了標籤

<div class="content-body attribute-pdf"> 
<a href="/_fragment/content/download/296/1935/file/blabla.pdf"> 
blabla.pdf</a> 1.2 Mb 
</div> 

這是走出來一個CMS,我想隱藏這個「1.2 MB」,但仍保持在A href部分

這是可以做jQuery的嗎?

我嘗試這樣做:

$(".attribute-pdf").children().hide(); 

,它可以隱藏A HREF,但仍顯示的文本。反之亦然 - 隱藏文字,但仍然顯示A href。

+0

或完全刪除文字也不是問題 –

回答

3

一個快速的方法,在jQuery的 - 空格,與只是<a>標籤替換它的內容:

$('.attribute-pdf').each(
    function() { 
    var container = $(this); 
    var a = container.find('a').detach(); 
    container.empty().append(a); 
    } 
); 

例子:http://codepen.io/paulroub/pen/iaFnK

+0

完美!謝謝!事實上,它應該在多個div上工作,並且這是最好的解決方案 –

1

你可以設置父的內容是孩子的內容...

$(".attribute-pdf").each(function(){ 
    var $this = $(this); // cache for performance 
    $this.html($this.children()); 
}); 
+0

如果存在多個'.attribute-pdf' div,那麼這將是有問題的。 –

+0

非常真實,我已更新以解釋此事。 – Ben

1

抓取內容(鏈接),空在div(除去1.2 MB),並再次追加的鏈路。

http://jsfiddle.net/vpVMK/

var content = $(".attribute-pdf a"); 
    $(".attribute-pdf").html(''); 
    $(".attribute-pdf").append(content); 
1

你可以這樣做:

// contents() gives children, all including non-element nodes. 
// Then, we can filter those down to the final text one. 
var textNodes = $(".attribute-pdf").contents().filter(function() { 
    return this.nodeType === 3; 
}); 

var lastTextNode = textNodes.last(); 
//and replace 
lastTextNode.replaceWith(''); 
0

這裏是尚未發佈的又一方法:

$(".attribute-pdf").html(function(){ 
    var $this = $(this), 
     $tmp = $('<div>'); 

    return $.makeArray($this.children('a').map(function() { 
     $tmp.html(this) 
     return $tmp[0].innerHTML 
    })).join('') 
}) 

fiddle