简体   繁体   English

Javascript 过滤 HTMLCollection

[英]Javascript filter over HTMLCollection

I want to get particular query params from all the js files which have been loaded by the browser.我想从浏览器加载的所有 js 文件中获取特定的查询参数。 My query params reside in the js file named script.js (ex: https://abcd.com/script.js?code=123 ).我的查询参数位于名为script.js的 js 文件中(例如: https://abcd.com/script.js?code=123 )。 In the browser console, I tried like this:在浏览器控制台中,我尝试这样:

const scripts = document.getElementsByTagName("SCRIPT")
scripts.filter((script) => { return script.src.includes("script.js?code=")})

But this returns the error Uncaught TypeError: document.getElementsByTagName(...).filter is not a function .但这会返回错误Uncaught TypeError: document.getElementsByTagName(...).filter is not a function Please suggest the code changes or is there any better way to get the same?请建议代码更改或有更好的方法来获得相同的?

Methods like getElementsByTagName do not return an array rather a list of node. getElementsByTagName 之类的方法不返回数组,而是返回节点列表。 Just wrap your queryselector with brackets and the spread operator to create an array from the nodelist.只需用括号和扩展运算符包装您的查询选择器即可从节点列表创建一个数组。

const scripts = [...document.getElementsByTagName("SCRIPT")]
scripts.filter((script) => { return script.src.includes("script.js?code=")})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM