jQuery .focus doesn’t actually focus a newly created element

后端 未结 8 1889
清歌不尽
清歌不尽 2020-12-08 03:38

Let’s say I have this markup:

First paragraph

8条回答
  •  -上瘾入骨i
    2020-12-08 04:15

    There's no problem with your code, it's just that a paragraph or div tag can't receive focus. Focus can only be given to things you can interact with, such as links, input elements, textareas, etc.

    To scroll the window to this newly added element, you can use a plugin such as ScrollTo.

    On a side note, your code could be simplified a bit:

    var html = "

    New paragraph

    "; $("#content").append(html); $("#newP p").focus(); var html = "

    New paragraph

    "; $(html) .appendTo('#content') .focus() // or scrollTo(), now... ;

提交回复
热议问题