Estoy desarrollando asp.net core y pruebo en el navegador Edge (versión: 96.0.1054.62).
Envío una solicitud ajax y obtengo el contenido html, luego incrusté el bloque html en la página actual. Después de eso, configuré el foco en el elemento html en el bloque html. Pero no puedo ver el efecto de enfoque cuando llamo a la función setFcous .
En general, cuando configuro el foco en un elemento, tendrá un borde negro que lo envuelve como el siguiente.
El siguiente es el ejemplo de código:
Índice.cshtml
<form> <input id="btnDelete" value="Delete" type="button" style="width: 60px" /> </form> <div id="divInputs" style="display: none"> </div> <script type="text/javascript"> function setFocus(elementId) { var obj = document.getElementById(elementId); if (isDefine(obj)) { console.log(obj); obj.focus(); } } function isDefine(obj) { return !(obj == undefined || obj == null) } $(function () { $('#btnDelete').on('click', function () { $.get('/Home/Delete?', (data) => { $('#divInputs').html($(data)); $('#divInputs').show(); setFocus('btnAction'); }); }); }); </script>HomeController.cs
public IActionResult Delete() { return View("Delete"); }Eliminar.cshtml
@{ Layout = null; } <div> <input id="btnAction" class="button" type="button" value="Delete"/> <input id="btnCancel" class="button" value="Cancel" type="button"/> </div>El foco se establece en el botón de su código. Puede probarlo agregando el siguiente estilo a su página.
<style> input:focus { background-color: yellow; } </style>