如何从div获取行数?我应该从伯爵那里得到2分。我在jsfiddle上尝试了这个,但我无法计算。我所要做的就是得到总计数并将计数放在rowCount
div
<div id="__gvctl00_ctl33_g_1fce23cc_cb78_4993_8f8a_c3369f472afa_ReportGrid__div">
<table class="ms-WPBody" cellspacing="0" id="ctl00_ctl33_g_1fce23cc_cb78_4993_8f8a_c3369f472afa_ReportGrid" style="border-style:None;width:100%;border-collapse:collapse;width:100%;">
<tbody><tr class="ms-viewheadertr ms-vhltr">
<th class="ms-vh2" align="left" scope="col">Item</th><th class="ms-vh2" align="left" scope="col"> </th><th class="ms-vh2" align="left" scope="col"><a onclick="javascript:__gvctl00_ctl33_g_1fce23cc_cb78_4993_8f8a_c3369f472afa_ReportGrid.callback("0|0|/wEFDFdvcmtmbG93TmFtZQmCSxMfY/MQRgGNQMjtH6ms4WKDojWXqKHYKkdrUi0C|"); return false;" href="javascript:__doPostBack('ctl00$ctl33$g_1fce23cc_cb78_4993_8f8a_c3369f472afa$ReportGrid','Sort$WorkflowName')">Workflow</a></th><th class="ms-vh2" align="left" scope="col">Status</th><th class="ms-vh2" align="left" scope="col"><a onclick="javascript:__gvctl00_ctl33_g_1fce23cc_cb78_4993_8f8a_c3369f472afa_ReportGrid.callback("0|0|/wEFCUVudHJ5VGltZWJK5FwcSWabKTvGRw+GSCnlui6XteK5oxR2+e2cXtJw|"); return false;" href="javascript:__doPostBack('ctl00$ctl33$g_1fce23cc_cb78_4993_8f8a_c3369f472afa$ReportGrid','Sort$EntryTime')">Waiting since</a></th><th class="ms-vh2" align="left" scope="col"><a onclick="javascript:__gvctl00_ctl33_g_1fce23cc_cb78_4993_8f8a_c3369f472afa_ReportGrid.callback("0|0|/wEFFEN1cnJlbnRBY3Rpdml0eVRpdGxlkVg5pZyBZyiI8xdXZ5QTh61pFRK8yr4Y6G7G3fj/9GE=|"); return false;" href="javascript:__doPostBack('ctl00$ctl33$g_1fce23cc_cb78_4993_8f8a_c3369f472afa$ReportGrid','Sort$CurrentActivityTitle')">Current action</a></th>
</tr><tr>
<td class="ms-vb2"><a href="http://canada-dev.canada.com/teams/wf/_layouts/15/NintexWorkflow/Redirect.aspx?view=WorkflowTask&InstanceId=c051a9ce-8c56-40ed-af65-a69f6cf1af18&SPTaskID=9">a test</a></td><td class="ms-vb2"></td><td class="ms-vb2">TEST-legalTasks</td><td class="ms-vb2"></td><td class="ms-vb2">4/6/2015 9:18 AM</td><td class="ms-vb2"><a href="http://canada-dev.canada.com/teams/wf/_layouts/15/NintexWorkflow/Preview.aspx?ListId=9417bb8a-7811-4b04-80d6-507c788969f5&ItemId=1&WorkflowId=5bdd0cef-5ade-4a27-ad9b-f0cfc17e3bf7&InstanceId=c051a9ce-8c56-40ed-af65-a69f6cf1af18">Request approval</a></td>
</tr><tr class="ms-alternating">
<td class="ms-vb2"><a href="http://canada-dev.canada.com/_layouts/15/NintexWorkflow/Redirect.aspx?view=WorkflowTask&InstanceId=9440ac78-287f-43d2-bb58-aebbe9c64983&SPTaskID=3">How to handle in Nintex workflwo</a></td><td class="ms-vb2"></td><td class="ms-vb2">Nintex Task Workflow</td><td class="ms-vb2"></td><td class="ms-vb2">4/6/2015 2:05 PM</td><td class="ms-vb2"><a href="http://canada-dev.canada.com/_layouts/15/NintexWorkflow/Preview.aspx?ListId=331be81b-ce04-4930-819e-2ed17daf5acd&ItemId=2&WorkflowId=bff491a8-e0ef-4a12-aec1-8ebb4cd868db&InstanceId=9440ac78-287f-43d2-bb58-aebbe9c64983">Request approval</a></td>
</tr>
</tbody></table>
</div>
答案 0 :(得分:1)
您可以像这样获得表格中tr
元素的数量:
$(".ms-WPBody tr").not(":has(th)").length
这将找到所有tr
个元素,而不是一个孩子th
。
完整代码:
var rowCount = $(".ms-WPBody tr").not(":has(th)").length;
$(".rowCount").text("Num of rows: " + rowCount);
这表明:
行数:2
并且不必使用任何像“-1”这样的魔法数字,如果表格改变结构,可能会引入粗略的错误。
这是更新的jsfiddle:https://jsfiddle.net/quyk2dqg/7/
答案 1 :(得分:0)
试试这个,你的小提琴有一些错误,并没有加载jquery。它仍然需要添加jquery ui位置我相信。
alert('Num of rows: ' + $('table tr').length );
答案 2 :(得分:0)
记住-1来补偿标题
var rows = 0;
rows = $('table').find("tr").length - 1;
$('#rowCount').html('row');
alert('Num of rows: ' + rows );
答案 3 :(得分:0)
小提琴中出现了一些错误,它们是:
以下是我认为你想要的东西:
var parentX = $('.ms-vb2').position().left;
var rows = 0;
$('.ms-vb2').each(function(){
if( $(this).position().left === parentX ) ++rows;
});
$('.rowCount').html('Num of rows: ' + rows);
更新了小提琴:https://jsfiddle.net/quyk2dqg/6/
请记住,这些是非常松散的选择器,如果您能够将某些类添加到行中,那么您可以在不执行$.each
类似于$('.definitely-a-row-class').length
。