[英]jQuery: Retrieve attributes from several parents and store them into a single array
I have an HTML list structure generated with PHP that simulates the structure of my directory. 我有一个用PHP生成的HTML列表结构,它可以模拟目录结构。
If my directory looks like this… 如果我的目录如下:
• folder A
— file A-1
— file A-2
• folder A-A
— file A-A-1
— file A-A-2
• folder B
— file B-1
• folder B-A
— file B-A-1
— file B-2
my HTML looks this way: 我的HTML看起来是这样的:
<h2 data-url="folder_A">folder A</>
<ul>
<li data-url="file_A-1">file A-1</li>
<li data-url="file_A-2">file A-2</li>
<h2 data-url="folder_A-A">folder A-A</h2>
<ul>
<li data-url="file_A-A-1">file A-A-1</li>
<li data-url="file_A-A-2">file A-A-2</li>
</ul>
</ul>
<h2 data-url="folder_B">folder B</>
<ul>
<li data-url="file_B-1">file B-1</li>
<h2 data-url="folder_B-A">folder B-A</h2>
<ul>
<li data-url="file_B-A-1">file B-A-1</li>
</ul>
<li data-url="file_B-2">file B-2</li>
</ul>
What I want to achieve is to rebuild the complete URL of a <li>
file when you click on it, from the data-url
of its parent elements. 我想要实现的是单击它时从其父元素的
data-url
重建<li>
文件的完整URL。
For example, the complete URL of the file BA-1 is folder_B/folder_B-A/file_B-A-1
. 例如, 文件BA-1的完整URL为
folder_B/folder_B-A/file_B-A-1
。
In order to achieve this, I need to retrieve the data-url
from <h2 data-url="folder_A">folder A</>
, <h2 data-url="folder_B">folder B</>
, and <li data-url="file_B-A-1">file BA-1</li>
, join them into an array and separate them with slashes. 为了实现这一点,我需要检索的
data-url
从<h2 data-url="folder_A">folder A</>
<h2 data-url="folder_B">folder B</>
和<li data-url="file_B-A-1">file BA-1</li>
,将它们合并成一个数组,并用斜杠将其分开。
Here is the jQuery I have so far: 这是到目前为止的jQuery:
$('li').click(function(){
var parentUrl = $(this).parent().prev('h2').attr('data-url');
var thisUrl = $(this).attr('data-url');
alert(parentUrl+'/'+thisUrl);
});
My code only works for one parent, any idea how to retrieve all the data-url
of the higher <h2>
and join them into one array? 我的代码仅适用于一个父级,不知道如何检索更高的
<h2>
所有data-url
并将它们连接到一个数组中?
Well I actually just found a solution that works before posting the question, but still it might help others. 好吧,我实际上只是在发布问题之前找到了一种可行的解决方案,但仍然可以帮助其他人。 And there might be a better solution…
也许会有更好的解决方案……
I used array.push();
我用了
array.push();
var rawParentsUrl = [];
$(this).parents('ul').each(function(){
rawParentsUrl.push( $(this).prev('h2').attr('data-url') );
});
Complete code: 完整的代码:
$('li').click(function(){
var rawParentsUrl = [];
$(this).parents('ul').each(function(){
rawParentsUrl.push( $(this).prev('h2').attr('data-url') );
});
var thisUrl = $(this).attr('data-url'),
parentsUrl = rawParentsUrl.reverse().join('/'),
completeUrl = parentsUrl+'/'+thisUrl;
alert(completeUrl);
});
You can use the .map()
method. 您可以使用
.map()
方法。 It returns a jQuery object containing the results of calling the function on each element in the collection. 它返回一个jQuery对象,其中包含对集合中每个元素调用函数的结果。 You can turn this into an array using
.get()
. 您可以使用
.get()
将其转换为数组。
var rawParentsUrl = $(this).parents('ul').map(function() {
return $(this).prev('h2').data('url');
}).get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.