gpt4 book ai didi

Javascript : get all the object where id is like log_XXXX

转载 作者:可可西里 更新时间:2023-11-01 02:33:28 26 4
gpt4 key购买 nike

我需要获取 ID 与特定模式匹配的所有对象。我该怎么做?谢谢!

最佳答案

当前浏览器:

// DOM collection as proper array
const matches = Array.from(document.querySelectorAll('[id^=log_]'));

旧版浏览器:(IE9+)

// Use Array.prototype.slice to turn the DOM collection into a proper array
var matches = [].slice.call(document.querySelectorAll('[id^=log_]'));

jQuery:

$('[id^=log_]')

真正的旧浏览器,没有 jQuery:

var matches = [];
var elems = document.getElementsByTagName("*");
for (var i=0; i<elems.length; i++) {
if (elems[i].id.indexOf("log_") == 0)
matches.push(elems[i]);
}
//matches now is an array of all matching elements.

关于Javascript : get all the object where id is like log_XXXX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/783463/

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