[英]why is jQuery selector only selecting first hierarchy element on specific website?
I'm trying to select some elements from this website: https://www.pexels.com/我正在尝试 select 这个网站的一些元素: https://www.pexels.com/
For example I try this simple selector to match all the divs elements inside the div father container with "photos__column" class:例如,我尝试使用这个简单的选择器将 div 父容器内的所有 divs 元素与“photos__column”class 匹配:
$('div.photos__column div')
But as a result it only selects the first of all of them, why is this happening?但结果它只选择了第一个,为什么会发生这种情况?
Thanks in advance.提前致谢。
$
could be anything. $
可以是任何东西。
Does this site have jQuery at all?这个网站有 jQuery 吗? You can check it in the browser console via
您可以通过在浏览器控制台中检查它
$.fn.jquery
Querying with native DOM API使用原生 DOM 查询 API
document.querySelectorAll('div.photos__column div')
works.作品。
$('div.photos__column div')
return an array with all elements with this pattern "div.photos__column div"
but whene you call a function like text()
, the function text use only the firest rsult of array $('div.photos__column div')
返回一个数组,其中包含具有此模式的所有元素"div.photos__column div"
但是当您调用 function 之类的text()
时, function 文本仅使用数组的最触发结果
if you want to do an operation on all selected element, can you use:如果要对所有选定元素进行操作,可以使用:
$('div.photos__column div').each(function( index ) {
console.log( index + ": " + $( this ).attr("src") );
});
or if you want to select element by index, you can call element by hir index for example: $('div.photos__column div')[2]
或者如果您想按索引 select 元素,您可以按 hir 索引调用元素,例如:
$('div.photos__column div')[2]
can you use this query to get all img source from the home page:您可以使用此查询从主页获取所有 img 源吗:
var allimegs = document.querySelectorAll(".photo-item__img");
var newsrc = [] ;for(var i = 0;i<allimegs.length;i++){
newsrc[i] = allimegs[i].getAttribute("src");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.