document.getElementById(tdId).innerHTML=document.getElementById(tdId).innerHTML+"<IMG src=imgs/vasave.png border=0 width=16 height=16 onclick=\"vasubmit();\" onload=\"document.getElementById("+tdId.replace("id_","input_")+").focus();\" style='cursor:hand'>";
("id_","input_")).value);alert(document.getElementById(tdId.replace("id_","input_")).name);document.getElementById(tdId.replace("id_","input_")).focus();这样的代码运行后,弹出警告框后,会聚焦到<input>控件上。可是我把alert注释掉,就无法直接聚焦。代码如下:document.getElementById(tdId).innerHTML=document.getElementById(tdId).innerHTML+"<IMG src=imgs/vasave.png border=0 width=16 height=16 onclick=\"vasubmit();\" onload=\"document.getElementById("+tdId.replace("id_","input_")+").focus();\" style='cursor:hand'>";
("id_","input_")).value);//alert(document.getElementById(tdId.replace("id_","input_")).name);document.getElementById(tdId.replace("id_","input_")).focus();我的需求是直接聚焦,请问如何实现?

解决方案 »

  1.   

    try:
    document.getElementById(tdId).innerHTML=document.getElementById(tdId).innerHTML+" <IMG src=imgs/vasave.png border=0 width=16 height=16 onclick=\"vasubmit();\" onload=\"document.getElementById('"+tdId.replace("id_","input_")+"').focus();\" style='cursor:hand'>"; 
    //("id_","input_")).value); 这里是什么代码???//alert(document.getElementById(tdId.replace("id_","input_")).name); document.getElementById(tdId.replace("id_","input_")).focus();
      

  2.   

    可能是加载了图片,导致图片没有加载完毕之前focus了,等图片加载完以后js自动把focus给了图片。
      

  3.   

    对于此类无法确定页面加载情况却又要focus()或select()的问题,通常都是使用setTimeOut方法来解决,我的程序代码中也是这样使用的:window.setTimeout(function(){document.getElementById(inputId).select();},0);