jQueryscript选择器出现问题

时间:2011-03-29 14:46:36

标签: jquery find selector

我试着写一下抓住“Enter”-keypress的jQuery脚本,找到第一个与“findMva”类在同一个div上的A-tag,而不是另一个带有“findMva”类的子div

示例1:

  • 我专注于输入id =“input2”
  • 我按Enter
  • 我希望脚本点击带有“更多信息9 ...”
  • 文字的A标签

示例2

  • 我专注于输入id =“input4”
  • 我按Enter
  • 我希望脚本点击带有“更多信息5 ...”
  • 文字的A标签

jQuery-script(谁不是在开发示例1,但在示例2中没有...):

$("input").keydown(function (e) {  
    if (e.which == 13) {  
        $(this).closest(".findMva").find(".mva").not(".findMva > .mva").first().click();  
    }  
}

HTML的标记:

<div class="findMva">
    <div class="findMva">
        <input type="text" id="input1" />
        <a class="mva" id="a1" href="#">More info 1...</a>
        <a class="mva" id="a2" href="#">More info 2...</a>
    </div>
    <input type="text" id="input2" />
    <div>
        <div>
            <div class="findMva">
                <input type="text" id="input3" />
                <a class="mva" id="a3" href="#">More info 3...</a>
                <a class="mva" id="a4" href="#">More info 4...</a>
                <div class="findMva">
                    <input type="text" id="input4" />
                    <a class="mva" id="a5" href="#">More info 5...</a>
                    <a class="mva" id="a6" href="#">More info 6...</a>
                    <div class="findMva">
                        <input type="text" id="input5" />
                        <a class="mva" id="a7" href="#">More info 7...</a>
                        <a class="mva" id="a8" href="#">More info 8...</a>
                    </div>
                </div>
            </div>
            <a class="mva" id="a9" href="#">More info 9...</a>
            <a class="mva" id="a10" href="#">More info 10...</a>
        </div>
    </div>
</div>

有关如何编写jQuery选择器的任何建议吗?

1 个答案:

答案 0 :(得分:2)

我找到了解决方案:

$(document).ready(function () {
    $("input").keydown(function (e) {
        if (e.which == 13) {
          var o = $(this).closest(".findMva").clone();
            o.find(".findMva").remove();
          var myid = o.find(".mva").first().attr("id");
          $("#" + myid).focus();
        }
    });
});

在这种情况下,我依赖于A标签上的ID属性。

相关问题