我有这个html,里面有一个包含股价的表格。
<tr id='row-7'>
<td>Glencore</td>
<td>Share Price</td>
<td>360</td>
<td class='actions'>
<a href="http://localhost/ye/index.php/enterprise/addressbook/read/21345" class="edit_button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" role="button">
<span class="ui-button-icon-primary ui-icon ui-icon-document"></span>
<span class="ui-button-text"> View</span>
</a>
<a href="http://localhost/ye/index.php/enterprise/addressbook/delete/21345" class="delete_button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" role="button">
<span class="ui-button-icon-primary ui-icon ui-icon-document"></span>
<span class="ui-button-text"> Delete</span>
</a>
</td>
</tr>表中有许多行,我想获取所有行的所有a的最后几个片段,类名为.selected。
这是a <a href="http://localhost/ye/index.php/enterprise/addressbook/read/21345" class="edit_button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" role="button"> <span class="ui-button-icon-primary ui-icon ui-icon-document"></span> <span class="ui-button-text"> View</span> </a>
我想要读取的最后一个uri段是21345,但是我希望读取所有行,并获取最后一个uri,并将它们放在逗号分隔列表中。
这是我到目前为止所使用的jquery。
var last_uri_segments = $('.mytable tr.selected td:last-child').map(function () {
var href = $(this).attr("href");
var last_segment = href.substr(href.lastIndexOf('/') + 1);
return this.last_segment;
}).get().join(',');
alert(last_uri_segments);我的第一个问题是如何针对td的最后一个子级中的最后一个a。
更新
这是小提琴http://jsfiddle.net/4tpwq94b/2/
发布于 2014-08-25 02:11:36
像这样的怎么样?
var $selectedRows = $("table tr.selected");
var idsFound = [];
$selectedRows.each(function() {
var url = $(this).find("td").last().find("a").first().attr("href");
var lastSegment = url.substring(url.lastIndexOf("/") + 1, url.length);
idsFound.push(lastSegment);
});
console.log(idsFound.join(","));发布于 2014-08-25 01:53:05
像这样吗?
var uriList = $('tr.selected a').map(function(){
return this.href.split('/').reverse()[0];
}).get();现在,您只需使用uriList.join(',')使其成为逗号分隔的值。
DEMO
https://stackoverflow.com/questions/25478015
复制相似问题