How do I keep a form in focus when a new one is added?

*爱你&永不变心* 提交于 2019-12-11 20:21:18

问题


Here's a fiddle: FIDDLE

function blanker(){ 
    var c = $('.whatever').html().replace("thisWillBlank", "<form><input type='text' 
            id='thisWillBlank'></form>");
    $('.whatever').html(c); 
}

$('#alreadyBlanked').focus(); 

setTimeout(function(){blanker()}, 3000); 

Using JS/jquery/css I would just like for whichever form that has been clicked on, to remain in focus even if new ones appear. Thanks.


回答1:


You're replacing the elements, so the old element is no longer available, but as long as it has an ID, you can target that instead:

function blanker(){ 
    var active = document.activeElement.id;
    var c = $('.whatever').html().replace("thisWillBlank", "<form><input type='text' id='thisWillBlank'></form>");
    $('.whatever').html(c); 
    $('#'+active).focus();
}

FIDDLE



来源:https://stackoverflow.com/questions/20182545/how-do-i-keep-a-form-in-focus-when-a-new-one-is-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!