简体   繁体   English

使用Jquery / JavaScript从url哈希除以“ /”创建数组

[英]Create array from url hash divided by “/” using Jquery/JavaScript

I have a hash in the URL that I've dynamically created using checkboxes, on the page load/document ready I'm checking to see if a hash exists and then if it does I want to loop through each string and do something with them. 我使用复选框动态创建的URL中有一个哈希,在页面加载/文档准备就绪时,我正在检查哈希是否存在,然后是否要遍历每个字符串并对它们进行处理。

for example my hash looks like these below. 例如我的哈希如下所示。

#thing1/thing2/thing3/thing4/thing5/thing6/thing7/thing8/

#thing1/thing2/thing3/

#thing1/thing3/thing5/thing7/thing9/

The first thing I do is remove the actual hash 我要做的第一件事是删除实际的哈希

window.location.hash.substring(1);

Now I have something like: 现在我有类似的东西:

thing1/thing2/thing3/

I want to get thing1, thing2 and thing3 and store them in a format I can loop over, maybe like this: 我想获取thing1,thing2和thing3并以我可以循环的格式存储它们,也许像这样:

var urlStrings = ["thing1", "thing2", "thing3"];

I hope someone is able to help me solve this problem, any help/advice is much appreciated. 我希望有人能够帮助我解决这个问题,非常感谢您的帮助/建议。

使用.split函数,像这样

var urlStrings = window.location.hash.substring(1).split("/");

Split them like this: 像这样拆分它们:

var str = 'thing1/thing2/thing3';
str.split('/');

And you can use indexes to get them: 您可以使用索引来获取它们:

str[1];//thing2

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

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