鉴于以下Html:
<td>
<div class="preWorkHoursCell"> </div>
<div class="timelineCell" data-resourceid="@(resource.Id)" data-date="@(currentDate.ToString("yyyyMMdd"))"> </div>
<div class="postWorkHoursCell"> </div>
</td>
以及以下jQuery:
function createEvent(resourceId, startDate) {
var timelineCell = $(".timelineCell[data-resourceid=" + resourceId + "][data-date=" + startDate + "]");
console.log(timelineCell.length); // <-- This is 1
var preWorkHoursCell = $(timelineCell).closest(".preWorkHoursCell");
console.log(preWorkHoursCell.length); // <-- This is 0
}
如果给出timelineCell,我如何获得对preWorkHoursCell div的引用?
答案 0 :(得分:2)
您可以使用.prev(),
使用
var preWorkHoursCell = $(timelineCell).prev(".preWorkHoursCell");
OR
var preWorkHoursCell = $(timelineCell).closest('td').find(".preWorkHoursCell");