我的 html 中有一张包含 img 标签的图片。它位于类“image”的 div 中。在那个 div 里面是另一个带有“caption”类的 div。该“标题”div 包含一个段落。
如何使用 javascript 或 jQuery 删除“caption”div 中的段落并删除“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">
<img id="img-1" src="http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg" />
<div class="caption">
<p>DELETE THIS TEXT!</p>
</div>
</div>
<div class="image">
<img id="img-2" src="http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg" />
<div class="caption">
<p>DELETE THIS TEXT!</p>
</div>
</div>
此外,我如何离线使用 jQuery?我必须下载它吗?我该怎么做?
谢谢。
最佳答案
这将删除所有 p
的元素类 .caption
的内部元素.
$(".caption p").remove();
https://jsfiddle.net/wsv389td/
JQuery 可以通过从这里获取文件或最小文件来离线使用:https://jquery.com/download/
如果您将文件存储在与使用它的 HTML 文件相同的目录中,您将放置
<script src="<name-of-jquery>.js"></script>
如果它在一个 JS 文件夹中,该文件夹与您的 HTML(非常常见)位于同一目录中,您将放置:
<script src="js/<name-of-jquery>.js"></script>
其中任何一个都会进入您的 HTML 头部
关于javascript - 如何使用 JavaScript 或 jQuery 自动删除内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36511255/