![](/img/trans.png)
[英]JQuery detect lost focus to another element on the page but not lost focus of the browser
[英]On focus, focus on another element jquery
題:
為什么下面的代碼不起作用?
問題:
我有一個包含下拉菜單的菜單。 (兩級導航/ Firefox)當我瀏覽網站時,它將專注於第一級導航。 它不會進入下拉菜單的各個項目。 當我循環瀏覽chrome時,它只會循環瀏覽菜單的前兩個標簽。
我正在嘗試的代碼:
$('#cssmenu ul li').focus(function() {
$('#cssmenu li ul a').focus();
});
菜單鏈接: http : //www.casa.gov.au
要回答原始問題,您的代碼無法正常工作的原因之一是因為焦點事件在#cssmenu
訂單項的<a>
標記上觸發,但是您將焦點偵聽器附加到了<li>
標記。
這可能會完成焦點事件的原始意圖,但可能無法解決選項卡跳過問題:
$(function(){
$('#cssmenu ul li a').focus(function() {
var parentLi = $(this).parent();
if(parentLi.find("ul").length > 0){
parentLi.find("ul li:first a").focus();
}
});
});
我在這個jsFiddle http://jsfiddle.net/ryKZu/3/中進行了測試
編輯 :固定選擇器
$('#cssmenu > ul > li > a').focus(function () {
$(this).parent().find('ul li a').first().focus();
});
試試看 閱讀jQuery的選擇器: http : //api.jquery.com/child-selector/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.