简体   繁体   English

如何使用javascript或jquery将焦点放在第一个LI上?

[英]How can I set the focus on the first LI with javascript or jquery?

How can I set the focus on the first li with JavaScript or jQuery ? 如何使用JavaScriptjQuery将重点放在第一个li上? I am a beginner. 我是初学者。

Code: 码:

 $('ul li').each(function() { if ($(this).find('a').length > 0) { $(this).find('a').css('color', 'red'); $(this).find('a').focus() return false; } }); 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="dfruits" class="sinline"> <li><p class="StockBez">ROBO</p></li> <li><p class="StockBez">SQ</p></li> <li><p class="StockBez">NVDA</p></li> </ul> 

You can't put focus on a li element, unless you set its tabindex , because it's, by default, not a focusable element. 除非设置li元素的tabindex ,否则不能将focus放在li元素上,因为默认情况下它不是可聚焦的元素。 If you set its tabindex , then you can use: 如果设置其tabindex ,则可以使用:

/* Put focus on the first li element. */
$("#dfruits > li").first().focus();

Snippet: 片段:

 /* ----- JavaScript ----- */ $("#dfruits > li").first().focus(); 
 <!----- HTML -----> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="dfruits" class="sinline"> <li tabindex="-1"> <p class="StockBez">ROBO</p> </li> <li tabindex="-1"> <p class="StockBez">SQ</p> </li> <li tabindex="-1"> <p class="StockBez">NVDA</p> </li> </ul> 


Notes: 笔记:

  1. In the HTML code above, I set the tabindex of each li element to -1 . 在上面的HTML代码中,我将每个li元素的tabindex设置为-1 That means the li elements can only be given focus programmatically via JavaScript and not by hitting the tab key. 这意味着只能通过JavaScript编程方式赋予li元素焦点,而不是通过按下tab键。

  2. If your HTML has any actual anchor elements, as the JavaScript code you have provided implies, you can use $("#dfruits a").first().focus(); 如果您的HTML有任何实际的锚元素(如您提供的JavaScript代码所暗示),则可以使用$("#dfruits a").first().focus(); to set focus to such elements without using tabindex provided they have the href attribute set. 设置焦点到此类元素,而无需使用tabindex前提是它们具有href属性集。

  3. If you are all about speed in your code you had better use $("#dfruits").find("li") instead of $("#dfruits > li") , because the Sizzle engine tries to match selectors from right to left ( @Mark Schultheiss ' comment). 如果您只关心代码的速度,则最好使用$("#dfruits").find("li")代替$("#dfruits > li") ,因为Sizzle引擎会尝试将选择器从右向匹配左( @Mark Schultheiss的评论)。

First off, to set focus on a non-input element you need to set a tabindex. 首先,要将焦点设置在非输入元素上,您需要设置tabindex。 Secondly you have no a anchor elements thus select nothing here in your markup. 其次,你有没有a锚元素因此您的标记选择这里什么都没有。 Work around set a tabindex on all the li you wish to receive focus, here I just set it on the first one. 解决方法是在所有希望获得焦点的li上设置一个tabindex,在这里,我将其设置为第一个。

Here are some code snips to illustrate 这是一些代码片段来说明

$('#dfruits').find('li').first().attr( 'tabIndex', -1);// make it able to focus
$('#dfruits').find('li').first().focus();//set focus on li
$('#dfruits').find('li').find('a').first().focus();// set focus to first anchor

//using cached object
var firstAnchor = $('#dfruits').find('li').find('a').first();
firstAnchor.attr('href','#');
firstAnchor.css('color', 'red').focus();


// using each. set href not in markup
$('#dfruits').find('li').find('a').first().each(function() {
    $(this).attr('href','#');
    $(this).css('color', 'red');
    $(this).focus()
});

Now since you have p , we will actually use that... 现在既然有了p ,我们就可以实际使用它了……

 // using each $('#dfruits').find('li').find('p').first().each(function() { $(this).attr( 'tabIndex', -1);// make it able to focus $(this).css('color', 'red'); $(this).focus() }); 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="dfruits" class="sinline"> <li><p class="StockBez">ROBO</p></li> <li><p class="StockBez">SQ</p></li> <li><p class="StockBez">NVDA</p></li> </ul> 

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

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