简体   繁体   English

如何在之间移动 <td> 和 <tr> 元件?

[英]how to traverse between <td> and <tr> element?

I have a html table structure below: 我在下面有一个html表结构:

<table>     
    <tr><td>ABC</td></tr>
    <tr>
        <td><input class="inp"/></td>
        <td><input class="inp"/></td>
    </tr>
    <tr><td><input class="inp"/></td></tr>
    <tr><td><input class="inp"/></td></tr>
</table>
<button id="btn">Click me</button> 

Then my purpose is let the focus always go to next the input field in class "inp" by button click. 然后,我的目的是通过单击按钮使焦点始终移至“ inp”类中的下一个输入字段。 Here is my script: 这是我的脚本:

var focused = null;
$(".inp").focus(function () {
    focused = $(this);
}).first().focus();

$("#btn").click(function(e){
     if (focused && focused.length) { 
        //this only works horizontally. 
        focused.parent().next().find(".inp").focus();

        // this only works vertically. 
        //focused.closest("tr").next().find(".inp").focus();
     }
});

I want to focus the second line after the last element in the first row. 我想将第二行集中在第一行中的最后一个元素之后。 How do I combine those two statements? 如何合并这两个语句?

Try this : 尝试这个 :

HTML HTML

<table>
<tr>
    <td>ABC</td>
</tr>
<tr>
    <td>
        <input class="inp" />
    </td>
    <td>
        <input class="inp" />
    </td>
</tr>
<tr>
    <td>
        <input class="inp" />
    </td>
</tr>
<tr>
    <td>
        <input class="inp" />
    </td>
</tr>
</table>
<button id="btn">Click me</button>

JS JS

// initial just focus on first input
$('.inp').first().focus();
// get all input
var all = $('.inp');
// get first input index and add one number for next focus
var index = (all.index(all.first()) + 1);

$('#btn').click(function () {
  // if length of input same with current index
  // defined into 0, (goes bact to first input)
  // if does't wanna go back just remove this line
  if (index == all.length) index = 0;
  // focus next input
  all.eq(index).focus();
  index++;
});

DEMO DEMO

This code seems little nasty but it will do the job for you :D 这段代码看起来有点讨厌,但是它将为您完成工作:D

$(document).ready(function() {

 var focused = null;

 $(".inp").focus(function() {
     focused = $(this);
 }).first().focus();

 $("#btn").click(function(e) {
     if (focused && focused.length) {

         if ($(focused).closest('tr').find('td').length > 1 && $(focused).closest('td').next().length != 0) {

             $(focused).closest('td').next().find('.inp').focus();
         } else {

             $(focused).closest('tr').next().find('.inp').focus();

         }

     }
 });

});

And here is Plunker : http://plnkr.co/edit/Nm3wETpltRo6i7mAXPtj?p=preview 这是Plunker: http ://plnkr.co/edit/Nm3wETpltRo6i7mAXPtj? p =preview

I have a fiddle that is jumping indexes until you reach the last one. 我有一个小提琴在跳索引,直到您到达最后一个。 If you want you can modify it to also go backwards. 如果需要,可以对其进行修改以使其向后移动。

http://jsfiddle.net/gyebua9e/ http://jsfiddle.net/gyebua9e/

$('.inp').first().focus();

var index   = 0;
var $inputs = $('.inp').length;

$('#btn').click(function() {
   index += 1;
   $('.inp').eq(index).focus();
});

You do this by using the JQuery .index() and .eq() functions on a wrapped set of all the input elements. 您可以通过对所有输入元素的包装集使用JQuery .index().eq()函数来执行此操作。

This moves the focus back to the first input when it is on the last input and the button is clicked. 当焦点位于最后一个输入并单击按钮时,会将焦点移回到第一个输入。

$(function() {
    var $inputs = $('.inp');

    var index = null;

    $inputs.focus(function() {
        index = $inputs.index($(this));
    });

    $inputs.first().focus();

    $('#btn').click(function(e) {
         if (index != null) {
             index = (index + 1) % $inputs.length;
             $inputs.eq(index).focus();
         }
    });
});

jsfiddle 的jsfiddle

This version does not wrap the focus back to the first input. 此版本未将焦点回绕到第一个输入。

$('#btn').click(function(e) {
     if (index != null) {
         if ((index + 1) < $inputs.length) {
             $inputs.eq(index + 1).focus();
         }
     }
});

jsfiddle 的jsfiddle

Sorry this is a little confusing, what are you trying to accomplish with jumping between input fields on click? 抱歉,这有点令人困惑,您要在点击时在输入字段之间跳转来尝试做什么?

Also are you just trying to always go to the next input no matter what the wrapper is? 另外,无论包装器是什么,您都只是试图始终转到下一个输入吗? so if I click the second input it goes to the third input, if I click the first it goes to the second input? 因此,如果单击第二个输入,它将转到第三个输入,如果单击第一个,它将转到第二个输入? etc 等等

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM