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].开发者_Go百科style.backgroundColor="transparent";
}
}
This is a year too late, but you CAN focus to a TD element. Just give it a tabindex property, and then do a focus().
This works for DIV elements and almost all others.
I've tried this on Firefox 3.5 and up.
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; 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.
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.
In your code you are really trying to get the focus on a tag TD, then the answer to the question is no.
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. With jQuery, you would do this:
$("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; you should only use a unique ID value once per page. 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.
精彩评论