如何找到所有.jpg,.png和.gif并使用JavaScript将它们列入网格?(How to find all .jpg, .png and .gif and list them into a g

编程入门 行业动态 更新时间:2024-10-10 12:17:06
如何找到所有.jpg,.png和.gif并使用JavaScript将它们列入网格?(How to find all .jpg, .png and .gif and list them into a grid with JavaScript?)

我想构建一个基本的书签 ,找到网页的所有.jpg,.png和.gif图像,并将它们列入网格。 (例如,连续4张图片)我刚刚找到了这个片段,但无论扩展名如何,它都会推送所有图片:

var images = document.getElementsByTagName('img'); var srcList = []; for(var i = 0; i < images.length; i++) { srcList.push(images[i].src); }

怎么做?

I would like to build a basic bookmarklet that finds all .jpg, .png and .gif images of a web page and list them into a grid. (e.g. 4 images in a row) I just found this snippet but it pushes all image no matter of the extension:

var images = document.getElementsByTagName('img'); var srcList = []; for(var i = 0; i < images.length; i++) { srcList.push(images[i].src); }

How to do it?

最满意答案

您可以使用querySelectorAll并使用正则表达式来获取具有扩展名的图像:

var images = document.querySelectorAll('img[src$=".jpg"], img[src$=".png"], img[src$=".gif"]'); var srcList = []; for(var i = 0; i < images.length; i++) { srcList.push(images[i].src); }

要生成您可以执行的图像列表:

for(var i = 0; i < images.length; i++) { var img = document.createElement('img'); img.src = images[i].src; document.body.appendChild(img); }

您必须添加一些CSS才能使其成为网格。

You could use the querySelectorAll and use a regular expression to get the images with the extensions:

var images = document.querySelectorAll('img[src$=".jpg"], img[src$=".png"], img[src$=".gif"]'); var srcList = []; for(var i = 0; i < images.length; i++) { srcList.push(images[i].src); }

To generate a list of images you could do:

for(var i = 0; i < images.length; i++) { var img = document.createElement('img'); img.src = images[i].src; document.body.appendChild(img); }

You have to add some CSS to make it a grid.

更多推荐

本文发布于:2023-08-07 02:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458396.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   gif   jpg   png   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!