【问题标题】:Find next element by class in jQuery在jQuery中按类查找下一个元素
【发布时间】:2016-01-19 10:39:35
【问题描述】:

首先我已经解决了一些与我的标题相同的问题,例如this 等,但没有一个解决我的问题。

HTML部分-

<tr id="row_question_container">
    <td valign="top" colspan="2">
        <div id="at_test_area-1" class="at_test_area">
            <div id="at_questions_container">
                <div id="1" class="question_block completed unmarked" style="display: none;">
                <!-- Question / Option / Settings etc in nested tables are here -->
                </div>
                <div id="2" class="question_block completed marked">
                <!-- Question / Option / Settings etc in nested tables are here -->
                </div>
                <div id="3" class="question_block incomplete unmarked" style="display: none">
                <!-- Question / Option / Settings etc in nested tables are here -->
                </div>
                <div id="4" class="question_block incomplete unmarked" style="display: none">
                <!-- Question / Option / Settings etc in nested tables are here -->
                </div>
            </div>
        </div>
    </td>
</tr>

我想要实现的是获得具有类incompletemarkeddiv 的下一个/壁橱id(使用下一个和上一个按钮导航)。在阅读了类似的问题后,我尝试关注 jQuery,但它返回了undefined

var marked_question = $('#at_questions_container').next('.marked').attr('id');
alert(marked_question);

【问题讨论】:

  • 问题是你在容器级别工作,而next() 工作在兄弟姐妹上,所以你应该查看当前可见的子元素并在上面使用next()

标签: jquery class element next


【解决方案1】:

对于后代,您应该使用find()children(),具体取决于您希望去的深度

var marked_question = $('#at_questions_container').find('.marked').attr('id');
console.log(marked_question);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
<tr id="row_question_container">
    <td valign="top" colspan="2">
        <div id="at_test_area-1" class="at_test_area">
            <div id="at_questions_container">
                <div id="1" class="question_block completed unmarked" style="display: none;">
                <!-- Question / Option / Settings etc in nested tables are here -->
                </div>
                <div id="2" class="question_block completed marked">
                <!-- Question / Option / Settings etc in nested tables are here -->
                </div>
                <div id="3" class="question_block incomplete unmarked" style="display: none">
                <!-- Question / Option / Settings etc in nested tables are here -->
                </div>
                <div id="4" class="question_block incomplete unmarked" style="display: none">
                <!-- Question / Option / Settings etc in nested tables are here -->
                </div>
            </div>
        </div>
    </td>
</tr>
</table>

【讨论】:

  • 谢谢。我明白了,但是因为这将返回第一个 marked ,所以只有如何导航到下一个/最近标记的?
  • @Dr.AtulTiwari,使用find 后,您可以像$('#at_questions_container').find('.marked').next('.marked') 一样使用next
  • 我测试了.next('.marked'),但我认为它作为对象返回,在控制台中我得到的输出为 - [div#3.question_block.incomplete.marked, div#4.question_block.incomplete.marked, prevObject: jQuery.fn.jQuery.init[3], context: document]
  • @Dr.AtulTiwari,对,如果你想要 id,你需要包含 attr('id'),比如 $('#at_questions_container').find('.marked').next('.marked').attr('id')
  • @Dr.AtulTiwari,很抱歉,我现在正在工作。如果您仍然不明白,请告诉我,我可以在今天晚些时候或明天为您提供帮助
【解决方案2】:

试试这个:

var marked_question = $('#at_questions_container').find('.marked:first').attr('id');

你想找到 at_questions_container 的后代,第一个选择器会给你找到的第一个。 next 作用于兄弟元素。

【讨论】:

    【解决方案3】:

    尝试:

    var marked_question =$('#at_questions_container').find('.marked').first().attr('id');
    alert(marked_question);
    

    【讨论】:

    • 你如何从first() 到下一个?请更具体。
    • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
    • varmarked_question =$('#at_questions_container').find('.marked').first(); var nextValue = mapped_question.next().attr('id'); console.log(nextValue);
    • 编辑您的答案并将该代码放在那里,而不是在评论中。它将提高您答案的价值(而且它会更具可读性)。另外,考虑添加解释。不鼓励在 SO 上仅提供代码答案。
    【解决方案4】:

    jQuery 的next() 函数将查找#at_questions_containersiblings,而不是子元素。

    由于您正在寻找#at_questions_container 的子代,您应该使用children() 函数,并结合:first 选择器:

    var theID = $('#at_questions_container').children('.marked:first').attr('id');
    

    使用children() 是一种更安全的方法,因为它只会搜索一个深度,从而防止返回任何也具有.marked 类的子元素。例如,来自 jQuery 文档:

    .children() 方法与 .find() 的不同之处仅在于 .children() 在 DOM 树中向下移动一层,而 .find() 可以遍历 向下多个级别选择后代元素(孙子, 等等)。


    作为对上述内容的补充说明,:first 选择器的使用不是必需的。调用attr('id')时,jQuery会自动返回集合中第一个元素的ID属性。

    【讨论】:

    • :first 并不是真正需要的,因为当您调用 attr('id') 时,它会默认获得第一个
    • 确实如此,但我将其包括在教育之外。
    • 感谢您的解释,但是由于这将只返回第一个标记,如何导航到下一个/最近标记?
    • @Dr.AtulTiwari 下一个/最接近什么
    • 我的意思是下一个标记的元素/div,但无论如何我是从另一条评论中得到的,我最终必须使用next('.marked')
    猜你喜欢
    • 2011-04-09
    • 2011-07-09
    • 2010-12-20
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2013-04-30
    相关资源
    最近更新 更多