获取 DOM 问题中上一个表的 ID
当我使用下面的 fn 时,它会像以前一样返回当前表。
我“调整”了切片中的索引以获得我正在寻找的结果。这是一个黑客行为,如果我遗漏了什么,我正在寻找解释/答案。
每个表都嵌套在一个或 2 个 div 中。但是使用选择器,我会假设前一个表是指定选择器的前一个表。
破解: ? $all.slice(0, $all.index(this)-10).reverse() : $all.slice($all.index(this) + 2)
原始函数:
$.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;
};
});
元素:这些是循环的,我试图使用向上、向下从一个表导航到下一个表、左右方向键..并且需要能够获取上一个&的ID下一张表
"<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'>"
+ " <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 + "'>"
作者致谢: jQuery 查找与表达式匹配的所有先前元素
另一个问题.. 这在 IE 中会爆炸!知道为什么吗?
$.fn.reverse = function () {
return this.pushStack(this.get().reverse(), arguments);
};
// 创建两个新函数:prevALL 和 nextALL。 $.each(['上一个', '下一个'], 函数 (unusedIndex, 名称) { $.fn[名称 + 'ALL'] = 函数 (matchExpr) { // 获取body中的所有元素,包括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;
};
});
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'>"
+ " <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;
};
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也尝试
一下,如果您正在循环,我会将您的 id 更改为类,因为每页只允许 1 个唯一 id。
try
also i would change your ids to classes if you are looping as only 1 unique id is allowed per page.