jQuery.专注在ie11中不起作用

jQuery.focus not working in I.E 11

本文关键字:不起作用 ie11 专注 jQuery      更新时间:2023-09-26

我想从URL中得到focus上的anchor tag

锚标记

<a id='714895'>&nbsp;</a>

http://test.com/sites/alzheimers-disease-early-detection.aspx?ShowAllComments=True # 714895

jQuery代码

 try
{
if(document.URL.indexOf("?ShowAllComments=True#") >= 0){
var elementClicked = "#" +location.href.split('#')[1];
$("elementClicked").focus();
}
catch(e)
{alert(e.message);}

Also tried

 try
{
if(document.URL.indexOf("?ShowAllComments=True#") >= 0){
var elementClicked = "#" +location.href.split('#')[1];
$("elementClicked").focus().focus();
}
catch(e)
{alert(e.message);}

 try
{
if(document.URL.indexOf("?ShowAllComments=True#") >= 0){
setTimeout(function(){
var elementClicked = "#" +location.href.split('#')[1];
$("elementClicked").focus();},100);
}
}
catch(e)
{alert(e.message);}

代码在FirefoxChrome上工作完美,但在I.E上不工作。任何其他的方式或修改应该做的工作在i.e. .

尝试:

window.setTimeout(function(){ 
var elementClicked = "#"+location.href.split('#')[1]; 
$('#elementClicked').focus(); }, 100);

IE 11时间对其他控件进行聚焦,然后窃取焦点并将其添加到elementClicked。

否则试试这个

try { 
function elementFocus(e){
if(document.URL.indexOf("?ShowAllComments=True#") >= 0){ 
var elementClicked = "#" +location.href.split('#')[1];
$("elementClicked").focus(); 
e.preventDefault(); 
} 
} 
catch(e){alert(e.message);}