Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery隐藏内部html,但标记除外_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery隐藏内部html,但标记除外

Javascript jQuery隐藏内部html,但标记除外,javascript,jquery,html,Javascript,Jquery,Html,我有以下HTML: <div class="content-body attribute-pdf"> <a href="/_fragment/content/download/296/1935/file/blabla.pdf"> blabla.pdf</a> 1.2 Mb </div> 这将隐藏A href,但仍显示文本。我希望它反之亦然-隐藏文本,但仍然显示A href。快速方式,在jQuery中-清空div,用替换其内容快速方式,在jQuer

我有以下HTML:

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

这将隐藏A href,但仍显示文本。我希望它反之亦然-隐藏文本,但仍然显示A href。

快速方式,在jQuery中-清空div,用
替换其内容快速方式,在jQuery中-清空div,用
替换其内容您可以将父级的内容设置为子级的内容

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

您可以将父对象的内容设置为子对象的内容

$(".attribute-pdf").each(function(){
    var $this = $(this); // cache for performance
    $this.html($this.children());
});
您可以这样做:

var children = $(".attribute-pdf").children();
$(".attribute-pdf").html(children);
您可以这样做:

var children = $(".attribute-pdf").children();
$(".attribute-pdf").html(children);

抓取内容(链接),清空div(删除1.2MB),然后再次附加链接

抓取内容(链接),清空div(删除1.2MB),然后再次附加链接

你可以做:

// 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('');
你可以做:

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

以下是另一个尚未发布的方法:

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

    return $.makeArray( $this.children('a').map(function() {
        $tmp.html(this)
        return $tmp[0].innerHTML
    }) ).join('')
})
$(“.attribute pdf”).html(函数(){
变量$this=$(this),
$tmp=$('');
返回$.makeArray($this.children('a').map(function()){
$tmp.html(这个)
返回$tmp[0]。innerHTML
}))。加入(“”)
})

这里是另一个尚未发布的方法:

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

    return $.makeArray( $this.children('a').map(function() {
        $tmp.html(this)
        return $tmp[0].innerHTML
    }) ).join('')
})
$(“.attribute pdf”).html(函数(){
变量$this=$(this),
$tmp=$('');
返回$.makeArray($this.children('a').map(function()){
$tmp.html(这个)
返回$tmp[0]。innerHTML
}))。加入(“”)
})

或完全删除文本也不是问题,或完全删除文本也不是问题,如果有多个
.attribute pdf
div,则会有问题。如果有多个
.attribute pdf
div,则会有问题。完美!谢谢事实上,它应该适用于多个div,这是最好的解决方案完美!谢谢事实上,它应该适用于多个div,这是最好的解决方案