获取 DOM 问题中上一个表的 ID

发布于 2024-10-25 03:14:35 字数 3850 浏览 5 评论 0原文

当我使用下面的 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)'>&nbsp&nbsp</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枉心 2024-11-01 03:14:35

也尝试

$("table").parent("#divOuter").prev("#divOuter").find("table"); //up one

$("table").parent("#divOuter").next("#divOuter").find("table"); //down one

一下,如果您正在循环,我会将您的 id 更改为类,因为每页只允许 1 个唯一 id。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文