gpt4 book ai didi

javascript - 如何找到位于多个同名类中的所有图像的来源?

转载 作者:行者123 更新时间:2023-11-30 16:19:20 25 4
gpt4 key购买 nike

我想检索类cheese 中所有图像的来源。文档中只会存在 4 个名为 cheese 的类。这些类中的每一个都只包含 1 张奶酪图片。

我昨天才开始学习 JavaScript,因为我需要将它用于我正在处理的使用 CefSharp 的 C# 项目。因此,如果我的某些条款不准确,我深表歉意。

以下代码将检索文档中前 4 个图像的来源。那4张图都是奶酪的通缉图。但是,如果文档中鲜花的图片位于奶酪图片的上方,那么它会取而代之的是检索 3 张鲜花图片和 1 张奶酪图片。我不想这样,因为它太不准确了。

<!DOCTYPE html>
<html>

<body>

<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/Pacific_Rock.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/oak_smoked.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/labne.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/exho.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/146500lz.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/90926mrdv3ch8z.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/91317lz.jpg"></div>

<script>
<!-- Stores a collection of all <img> elements in the document in a variable called pictures -->
var pictures = document.images;

<!-- Stores the first <img> element in the document in a variable called firstPicture -->
var firstPicture = pictures[0].src;

var secondPicture = pictures[1].src;
var thirdPicture = pictures[2].src;
var fourthPicture = pictures[3].src;

<!-- Displays an alert box showing the first, second, third, and fourth <img> element in the document -->
alert(firstPicture + "\n" + secondPicture + "\n" + thirdPicture + "\n" + fourthPicture)
</script>

</body>

</html>

以下代码将通过定位 content 类中的所有图像来更准确地检索 4 个奶酪图像的来源。即使在文档中鲜花图像位于奶酪图像之上,以下代码仍将实现主要目标。

<!DOCTYPE html>
<html>

<body>
<div class="content">
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/Pacific_Rock.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/oak_smoked.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/labne.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/exho.jpg"></div>
</div>

<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/146500lz.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/90926mrdv3ch8z.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/91317lz.jpg"></div>

<script>
var pictures = document.getElementsByClassName('content')[0];
var firstPicture = pictures.getElementsByTagName('img')[0].src;
var secondPicture = pictures.getElementsByTagName('img')[1].src;
var thirdPicture = pictures.getElementsByTagName('img')[2].src;
var fourthPicture = pictures.getElementsByTagName('img')[3].src;
alert(firstPicture + "\n" + secondPicture + "\n" + thirdPicture + "\n" + fourthPicture)
</script>
</body>

</html>

上面的 2 个代码块只是示例。 真实文档的代码如下:

<!DOCTYPE html>
<html>
<body>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/146500lz.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/90926mrdv3ch8z.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/91317lz.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/Pacific_Rock.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/oak_smoked.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/labne.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/exho.jpg"></div>
<script>
var pictures = document.images;
var firstPicture = pictures[0].src;
var secondPicture = pictures[1].src;
var thirdPicture = pictures[2].src;
var fourthPicture = pictures[3].src;
alert(firstPicture + "\n" + secondPicture + "\n" + thirdPicture + "\n" + fourthPicture)
</script>
</body>
</html>

如何准确定位 4 个 cheese 类并返回类内所有图像的来源?每个类中只会有 1 个奶酪图像,我想将每个图像的值存储到一个变量中,并将它们显示在警告框中。我不想让你们把代码交给我,仅此而已。我想要解释一切如何运作。

感谢您花时间阅读我的问题! :)

这个问题已经得到解答,感谢 jfriend00。

我正在使用以下代码来实现预期的结果:

<!DOCTYPE html>
<html>

<body>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/146500lz.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/90926mrdv3ch8z.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/91317lz.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/Pacific_Rock.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/oak_smoked.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/labne.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/exho.jpg"></div>

<script>
var imgs = document.querySelectorAll(".cheese img");
var img1 = imgs[0].src;
var img2 = imgs[1].src;
var img3 = imgs[2].src;
var img4 = imgs[3].src;
alert(img1 + "\n" + img2 + "\n" + img3 + "\n" + img4)
</script>

</body>

</html>

最佳答案

您可以使用选择器查询来获取目标图像:

var imgs = document.querySelectorAll(".cheese img");
for (var i = 0; i < imgs.length; i++) {
console.log(imgs[i].src);
}

这会找到任何 <img> class="cheese" 对象的子标签然后遍历它找到的 imgs 集合。 document.querySelectorAll()返回 nodeList 这是一个类似数组的对象(它上面没有实际的 Array 方法,但它确实有 .length 并且可以使用 [i] 进行索引)。

您可以阅读更多关于 document.querySelectorAll() 的信息here on MDN .它需要一个 CSS selector string作为输入,并将返回所有匹配 DOM 元素的节点列表。

如果你想要 .src数组中的 URL,您可以这样做:

var imgs = document.querySelectorAll(".cheese img");
var srcArray = [];
for (var i = 0; i < imgs.length; i++) {
srcArray.push(imgs[i].src);
}
// srcArray holds the URLs here

或者,更高级一点:

var srcArray = Array.prototype.map.call(document.querySelectorAll(".cheese img"), function(img) {
return img.src;
});

这是一个工作片段:

var srcArray = Array.prototype.map.call(document.querySelectorAll(".cheese img"), function(img) {
return img.src;
});

document.getElementById("results").innerHTML = srcArray.join("<br>");
#results {
margin-bottom: 30px;
}
<div id="results"></div> 
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/146500lz.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/90926mrdv3ch8z.jpg"></div>
<div class="flowers"><img src="http://cdn1.1800flowers.com/wcsstore/Flowers/images/catalog/91317lz.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/Pacific_Rock.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/oak_smoked.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/labne.jpg"></div>
<div class="cheese"><img src="http://www.cheese.com/media/img/cheese/exho.jpg"></div>

关于javascript - 如何找到位于多个同名类中的所有图像的来源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34979637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com