简体   繁体   English

跳到HTML中尚不可见的元素

[英]Tabbing to an element which is not yet visible in HTML

I have a table with three rows. 我有一张三行的桌子。 Each row has a label and a text-box. 每行都有一个标签和一个文本框。 The second row is initially hidden. 第二行最初是隐藏的。

The code below will make the second row visible if the number in the first text box is more than one. 如果第一个文本框中的数字大于一个,则下面的代码将使第二行可见。

However, if one enters a number in the first box, and presses TAB, then the cursor is moved to the third text box - because the second text box is not yet visible. 但是,如果在第一个框中输入数字并按TAB键,则光标将移至第三个文本框-因为第二个文本框尚不可见。

Is there a neat way of getting the cursor to TAB to the second text box, in the event that the second row will be made visible? 如果第二行将可见,是否有一种巧妙的方法将光标移到第二个文本框的TAB上?

Thanks 谢谢

<SCRIPT>
function changePrinciplePayments(aVal) {
    if (aVal > 1) {
        $('#paymentFrequency').show();
    } else {
        $('#paymentFrequency').hide();
    }
}
</SCRIPT>



 <table>
    <tr class="Blocks">
            <td>Number of Payments:</td>

        <td><input class="Form80" id="numPrincipalPayments" onblur=
        "changePrinciplePayments(this.value);" tabindex="18" type=
        "text"></td>
    </tr>

    <tr class="Blocks" id="paymentFrequency" style="display:none;">
        <td>Payment Frequency:</td>

        <td><input tabindex="19" type="text"></td>
    </tr>

    <tr class="Blocks" id="paymentType">
        <td>payment Type:</td>

        <td><input tabindex="20" type="text"></td>
    </tr>
</table>

It is because of your tabindex, change it to the normal order. 这是因为您的tabindex,将其更改为正常顺序。 If a textfield is not displayed, it will be skipped in the order of tabs, so when it is not hidden, it will not be skipped. 如果未显示文本字段,则将按选项卡的顺序跳过该文本字段,因此当未隐藏该文本字段时,将不会跳过它。

Example of your table: 表的示例:

<table>
    <tr class="Blocks">
        <td>Number of Payments:</td>
        <td>
            <input class="Form80" id="numPrincipalPayments" onblur="changePrinciplePayments(this.value);" tabindex="18" type="text">
        </td>
    </tr>
    <tr class="Blocks" id="paymentFrequency" style="display:none;">
        <td>Payment Frequency:</td>
        <td>
            <input tabindex="19" type="text">
        </td>
    </tr>
    <tr class="Blocks">
        <td>Number of Payments:</td>
        <td>
            <input class="Form80" id="someId" tabindex="20" type="text">
        </td>
    </tr>
</table>

Tested this in Google Chrome and it is working as you would expect. 已在Google Chrome浏览器中对此进行了测试,并且可以正常运行。

EDIT 编辑

You want to focus to the second input on the event the second input is made visible, you can achieve this quite simply by setting the focus to the input. 您希望将焦点集中在第二个输入上,以使第二个输入可见,您可以通过将焦点设置为输入来非常简单地实现此目的。 You do this by editing your function to something like this: 您可以通过将函数编辑为以下内容来实现此目的:

function changePrinciplePayments(aVal) {
    var element = $('#paymentFrequency');
    if (aVal > 1) {
        element.show();
        element.find('input').focus();
    } else {
        element.hide();
    }
}

EDIT 2 编辑2

You can even do this in a better way by not listening to onblur , but on onchange , it would then look something like this: 您甚至可以通过不听onblur来更好地做到这一点,但是在onchange ,它看起来像这样:

Your row: 您的行:

<tr class="Blocks">
    <td>Number of Payments:</td>
    <td>
        <input class="Form80" id="numPrincipalPayments" onchange="changePrinciplePayments(this.value);" tabindex="18" type="text">
    </td>
</tr>

Your function: 您的功能:

function changePrinciplePayments(aVal) {
    var element = $('#paymentFrequency');
    if( !$.isNumeric(aVal) ) {
        element.hide();            
        return true;
    }

    if (aVal > 1) {
        element.show();
        element.find('input').focus();
    } else {
        element.hide();
    }

    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可见区域之外的HTML输入制表符会修改什么? - HTML Input Tabbing beyond visible area modifies what? 获取标签后具有焦点的“上一个”元素 - Get “previous” element which had focus after tabbing 尚不可用的 setTimeout 访问元素 - setTimeout access element which is not available yet 如果尚未添加HTML元素,则Jquery .click()不起作用 - Jquery .click() not work if HTML element not yet added 哪个元素抛出“ElementNotVisibleError:元素不可见”? - Which element is throwing the "ElementNotVisibleError: element not visible"? 检查以查看哪个元素在视口中可见(如果特定元素可见,则不可见) - Check to see which element is visible in viewport (not if a specific element is visible) 切换到元素之前更改元素的tabindex - Changing tabindex of element before tabbing into it Web 辅助功能 - 屏幕阅读器在鼠标 hover 和标签上以不同方式读取 HTML 元素 - Web Accessibility - Screen Reader reads HTML element differently on mouse hover and on tabbing 如果元素尚未显示,则html转到另一页的特定元素? - html go to the specific element of another page if the element is not shown yet? 识别具有溢出的元素的可见部分 - Identify visible portion of an element which has an overflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM