2016-04-09 37 views
1

我有一個圖像包含在我的html中的img標籤。它是與類「圖像」的div的內部。裏面的div是「caption」類的另一個div。該「標題」div包含一個段落。如何使用JavaScript或jQuery自動刪除內容

如何刪除「caption」div中的段落,並使用javascript或jQuery刪除「caption」div本身?

/* Script to delete the paragraph and caption div. */ 
 

 
$(".image img").each(function() { 
 
    var $this = $(this), 
 
     getClass = $this.attr('img') 
 
    splitClass = $this.prop('id').split("-") 
 
    if (splitClass[1] <= 20) { 
 
     $this.attr("src", "http://lorempicsum.com/futurama/350/200/1"); 
 

 
    } else if (splitClass[1] >= 40) { 
 
     $this.attr("src", "http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg"); 
 

 
    } else { 
 
     $this.attr("src", "http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg"); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="image"> 
 
\t <img id="img-1" src="http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg" /> 
 
\t <div class="caption"> 
 
\t \t <p>DELETE THIS TEXT!</p> 
 
\t </div> 
 
</div> 
 

 
<div class="image"> 
 
\t <img id="img-2" src="http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg" /> 
 
\t <div class="caption"> 
 
\t \t <p>DELETE THIS TEXT!</p> 
 
\t </div> 
 
</div>

此外,我怎麼能使用jQuery下線?我必須下載它嗎?我怎麼做?
謝謝。

+0

document.querySelector( '標題')的頭內取出()。 – Federico

+0

https://jquery.com/download/ – Federico

+0

@Federico在哪裏放? –

回答

1
$(".image img").each(function() { 

    var $this = $(this), 
     getClass = $this.attr('img'); 
    this.nextElementSibling.remove(); 
    splitClass = $this.prop('id').split("-") 
    if (splitClass[1] <= 20) { 
     $this.attr("src", "http://lorempicsum.com/futurama/350/200/1"); 

    } else if (splitClass[1] >= 40) { 
     $this.attr("src", "http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg"); 

    } else { 
     $this.attr("src", "http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg"); 
    } 
}); 
+0

對不起,但你可以看看這裏的問題https://jsfiddle.net/wsv389td/1/ –

+1

使用'this.nextElementSibling .remove();'代替 – Federico

+0

https://jsfiddle.net/14eu1gh6/ – Federico

2

這會刪除所有元素p內部類.caption的元素。

$(".caption p").remove(); 

https://jsfiddle.net/wsv389td/

的JQuery可以脫機通過獲取文件,或分檔,從這裏可以使用:https://jquery.com/download/

如果你使用它,你存儲的文件在同一目錄作爲HTML文件會把

<script src="<name-of-jquery>.js"></script>

如果是在是在同一目錄下你一個JS文件夾[R HTML(很常見的),你會放:

<script src="js/<name-of-jquery>.js"></script>

無論這些人會去的,你HTML

+0

對不起,但你可以看看這裏的問題https://jsfiddle.net/wsv389td/1/ –

+0

「我想使用JavaScript或jQuery刪除(.caption)和(p)「。這不是你要求的嗎?如果這不是你想要完成的,請在你的問題上更簡潔。 – theblindprophet

+0

謝謝!問題解決了! –

相關問題