开发者

insertbefore is working when I viewsource, but I don't see new dom element in browser?

I am inserting html before a specific element in a response to a ajax call.

I don't see the newly inserted html on the page, but if I do a view source I can see it.

I add the html in the success event o开发者_Go百科f the $.ajax method call.

success: function(data) {
  $("#some-id").insertBefore(data);

}

It isn't hidden either, so not sure what is happening?


(Expanding on my comment above)

Most likely, you want:

$("#some-id").before(data);

or:

$(data).insertBefore("#some-id");

Some more information from the documentation:

The .before() and .insertBefore() methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With .before(), the selector expression preceding the method is the container before which the content is inserted. With .insertBefore(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted before the target container.


If #some-id is not in your DOM by default and is being inserted dynamically, you'll need to append it before you can use insertBefore()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜