使用单个键向上功能调用来定位多个选项卡

时间:2016-12-01 22:15:05

标签: javascript jquery asp.net-mvc

我正在尝试构建一个DRY JQuery调用,它允许我在表单结构中定位几个选项卡。

JQuery的:

`<span>
<span class="sr-only" id="anchor" tabindex="0">Main Content</span> 
                        <span id="UniqueId1" tabindex="0" class="subTabAction">Details</span>
                        <span id="UniqueId2" tabindex="0" class="subTabAction">Duration</span>      
                        <span id="UniqueId3" tabindex="0" class="subTabAction"></span>
                        @if (Model.blah.Count > 1)
                        {
                            <span id="blah" tabindex="0" class="subTabAction"></span>
                        }
                        @*<span class="sr-only" id="anchor" tabindex="0">Main Content</span>*@ 
                    </span>`

HTML:

var motto = [];

var randomNum = Math.floor(Math.random()*10);

for (i = 0; i < 7; i++) { 

    motto.pop(randomNum);

}

for (i = 0; i < motto.length; i++) { 

    document.write(motto[i] + "<br>");
}

每个选项卡都有一个唯一的id,并触发ajax调用,问题是我只能这样做一个目标。我希望能够单独定位每个目标,以便只能使用键盘访问它们。

1 个答案:

答案 0 :(得分:3)

$(".subTabAction")是多个标签的集合,因为此类名称中包含更多元素。 $(this)会为您提供您定位的标签。

$(".subTabAction").keyup(function (event) {
    if (event.keyCode == 13) {
        $(this).click();
    }
});