2014-06-12 26 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