简体   繁体   English

如何关注一个 <tr> 要么 <td> 使用jQuery或JavaScript标记?

[英]How to focus a <tr> or <td> tag using jQuery or JavaScript?

for(var i=0;i<tr.length;i++){
    var td = tr[i].getElementsByTagName("td");
    if(td[2].innerHTML==time && td[3].innerHTML==cls){
        td[0].setAttribute("id","focus");
        tr[i].style.backgroundColor="green";
        var foc = document.getElementById("focus");
        foc.focus();
        cnt++;
    }
    else{
        tr[i].style.backgroundColor="transparent";
    }

 }

This is a year too late, but you CAN focus to a TD element. 这是一年为时已晚,但你可以专注于TD元素。 Just give it a tabindex property, and then do a focus(). 只需给它一个tabindex属性,然后做一个focus()。

This works for DIV elements and almost all others. 这适用于DIV元素和几乎所有其他元素。

I've tried this on Firefox 3.5 and up. 我在Firefox 3.5及更高版本上试过这个。

As the question is about changing the focus on a TD or TR, then the answer is that you cannot focus on such HTML tags; 由于问题是关于改变TD或TR的焦点,那么答案是你不能专注于这样的HTML标签; you can change the currently focused element if that is a input element (textarea, textfield, checkbox, radios) or a link, which are highlighted in a particular way when you click the key tab. 您可以更改当前聚焦的元素(如果它是输入元素(textarea,textfield,checkbox,radios)或链接,当您单击键选项卡时以特定方式突出显示该元素。
If you are really trying to focus on an input field, as suggested by Jonathan Sampson, then the answer he gave is the correct one. 如果你真的试图专注于输入领域,正如Jonathan Sampson所建议的那样,那么他给出的答案是正确的。

In your code you are really trying to get the focus on a tag TD, then the answer to the question is no. 在您的代码中,您真的试图将焦点放在标签TD上,那么问题的答案就是否定。

It appears you have an input field with the id "focus" within a td, so I'm assuming you want to focus on that input itself. 看来你在td中有一个id为“focus”的输入字段,所以我假设你想要专注于那个输入本身。 With jQuery, you would do this: 使用jQuery,你会这样做:

$("input#focus").focus();

This will cause the focus to be brought to that particular element once called. 这将导致焦点在被调用后被带到该特定元素。 I see that you also tried setting the ID of a particular TD to "focus" as well; 我看到你也尝试将特定TD的ID设置为“焦点”; you should only use a unique ID value once per page. 每页只能使用一次唯一的ID值。 If you feel the need to use a single value to represent many elements, consider using classnames instead as there can exist any number of them on a page. 如果您觉得需要使用单个值来表示许多元素,请考虑使用类名,因为页面上可以存在任意数量的类。

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

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