问题
This is my first post so let me know if you would like more information!
I am using a select list and a jQuery accordion. When the user selects a value from the list it opens up the relevant part of the accordion using the activate method.
This works fine apart from it also focusses the window on the accordion rather than leaving the user on in the same place.
Does anyone know how to prevent this?
回答1:
You may be able to store the currently active element, and restore focus after the user clicks on the accordion header.
You can retrieve the currently focused element using the following code:
function onElementFocused(e)
{
if (e && e.target)
document.activeElement =
e.target == document ? null : e.target;
}
if (document.addEventListener)
document.addEventListener("focus", onElementFocused, true);
This will keep the currently focused element in the document.activeElement variable.
Using the 'changestart' and/or 'change event with the accordion, you can then restore the focus to the desired element every time the accordion changes.
You may need to add a clause or two to prevent the accordion header divs being set as the current active element in the above code, otherwise you'll just restore focus to the accordion header.
The above code is untested, as is the idea, but it should work I believe.
来源:https://stackoverflow.com/questions/2584906/jquery-accordion-focus