如何聚焦动态创建的元素?
Just call .focus()
on the element after it's added to the DOM, for example:
var input = document.createElement("input"); //create it
document.body.appendChild(input); //append it
input.focus(); //focus it
The focus method will do this. If you have a reference to the newly-created element called elem
, then simply invoke:
elem.focus();
Note that you'll need to do this after inserting the element into the document at the appropriate point, of course.
Try this code,
var txtObj = document.createElement("input");
window.document.body.appendChild(a);
txtObj.focus();
<script>
$('#container').append('<input type="text">');
$('#container').find('input:last').focus();
</script>
You can use setTImeOut
setTimeout(function() {
$("#id_of_element_created").focus().select();
}, 100);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.