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

2019-08-30 11:37发布

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条回答
闹够了就滚
2楼-- · 2019-08-30 12:08

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

查看更多
登录 后发表回答