2016-04-09 20 views
1

私の画像にimgタグが含まれています。それはクラス「画像」を持つdivの内部です。そのdivの中には、「キャプション」のクラスを持つ別のdivがあります。その "キャプション" divには段落が含まれています。JavaScriptまたはjQueryを使用してコンテンツを自動削除する方法

「キャプション」divの段落を削除し、javascriptまたはjQueryを使用して「キャプション」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

これは、クラス.captionの要素内のpであるすべての要素を削除します。 https://jquery.com/download/

あなたがHTMLファイルと同じディレクトリにファイルを保存した場合、それを使用して:jQueryのはここから、ファイル、または最小のファイルを取得することにより、オフラインで使用することができます

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

https://jsfiddle.net/wsv389td/

<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

ありがとうございます!問題は解決しました! –

関連する問題