Фокус с текстовым полем, но курсор невидим

Я попытался получить фокус после того, как узел вставлен после:

<p>Insert new content</p>

var footnoteForm = $(`
<form class = "article-footnote-form footnote-form" style = "margin-top:10px">
<div class = "form-group row">
    <div class = "col-md-9">
        <textarea class = "form-control" name = "footnote" rows = "3" autofocus></textarea>
    </div>
    <div class = "col-md-3">
        <button class = "btn btn-primary" id = "articleFootnoteBtn">Add Annotation</button>
    </div>
</div>
</form>`);
//insert after
$("p").after(footnoteForm);

Когда я тестирую на консоли, чтобы получить фокус, курсор не появляется в текстовом поле:

$("textarea :first").focus()
jQuery.fn.init [textarea.form-control, prevObject: jQuery.fn.init(1)]
Как конвертировать HTML в PDF с помощью jsPDF
Как конвертировать HTML в PDF с помощью jsPDF
В этой статье мы рассмотрим, как конвертировать HTML в PDF с помощью jsPDF. Здесь мы узнаем, как конвертировать HTML в PDF с помощью javascript.
0
0
13
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Вам нужен $('textarea:first').focus() без места в селекторе. textarea :first означает textarea *:first - выбирает первый вложенный элемент в текстовом поле.

var footnoteForm = $(`
    <form class = "article-footnote-form footnote-form" style = "margin-top:10px">
    <div class = "form-group row">
    	<div class = "col-md-9">
    		<textarea class = "form-control" name = "footnote" rows = "3" autofocus></textarea>
    	</div>
    	<div class = "col-md-3">
    		<button class = "btn btn-primary" id = "articleFootnoteBtn">Add Annotation</button>
    	</div>
    </div>
    </form>`);
//insert after
$("p").after(footnoteForm);

$('textarea:first').focus()
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Insert new content</p>

Другие вопросы по теме