When i use the fn below it returns the current table as previous..
I "tweaked" the index in the slice to get the results I was looking for. This is a hack and I am looking for explanation/answer if there is something I am missing.
Each tables is nested in a div or 2. But using the selector I would assume the previous would be the previous of the selector specified.
hack: ? $all.slice(0, $all.index(this)-10).reverse() : $all.slice($all.index(this) + 2)
original func:
$.fn.reverse = function () {
return this.pushStack(this.get().reverse(), arguments);
};
// create two new functions: prevALL and nextALL.
$.each(['prev', 'next'], function (unusedIndex, name) {
$.fn[name + 'ALL'] = function (matchExpr) {
// get all the elements in the body, including the body.
var $all = $('body').find('*').andSelf();
// slice the $all object according to which way we're looking
$all = (name == 'prev')
? $all.slice(0, $all.index(this)).reverse()
: $all.slice($all.index(this) + 1)
;
// filter the matches if specified
if (matchExpr) $all = $all.filter(matchExpr);
return $all;
};
});
elements: these are looped, I am trying to navigate from one table to the next using up, down, left right arrow keys.. and need to be able to get the ID of the previous & next table
"<div id='divOuter" + div.divCode + "' class='BoxOuterDiv' >"
+ " <div id='div" + div.divCode + "' class='BoxDiv' >"
+ " <table class='grid' id='" + div.divCode + "' width='100%'>"
+ " <tr>"
+ " <td class='col0' width='125px' nowrap='nowrap'>"
+ " <span id='spn0" + div.divCode + "' class='rgCollapse' onclick='Collapse(this)'>  </span>"
+ " Division: " + div.divCode
+ " </td>"
+ " <td class='col1' width='5%' style='padding-left:15px' nowrap='nowrap'>"
+ " Description: "
+ " </td>"
+ " <td class='col2' width='40%' nowrap='nowrap'>"
+ " <input class='grid' type='text' id='dvDesc" + div.divCode + "' value='" + div.divDesc + "' style='width: 400px' maxlength='60' />"
+ " </td>"
+ " <td class='col3' width='50%' align='right' nowrap='nowrap'>"
+ " 开发者_如何学Go <input type='button' id='btnDv" + div.divCode + "' value='Add Business Unit' style='align:right' onclick='AddMfRow(this);' />"
+ " </td>"
+ " </tr>"
+ " </table>"
+ " </div>"
+ "<div id='divWrapper" + div.divCode + "'>"
Credit to Author: jQuery to find all previous elements that match an expression
Another question.. This blows up in IE! any idea why??
$.fn.reverse = function () {
return this.pushStack(this.get().reverse(), arguments);
};
// create two new functions: prevALL and nextALL. $.each(['prev', 'next'], function (unusedIndex, name) { $.fn[name + 'ALL'] = function (matchExpr) { // get all the elements in the body, including the body. var $all = $('body').find('*').andSelf();
// slice the $all object according to which way we're looking
$all = (name == 'prev')
? $all.slice(0, $all.index(this)-10).reverse()
: $all.slice($all.index(this) + 2)
;
// filter the matches if specified
if (matchExpr) $all = $all.filter(matchExpr);
return $all;
};
});
try
$("table").parent("#divOuter").prev("#divOuter").find("table"); //up one
$("table").parent("#divOuter").next("#divOuter").find("table"); //down one
also i would change your ids to classes if you are looping as only 1 unique id is allowed per page.
精彩评论