开发者

How to select recently appeded element in jquery?

I want to select recently created element doing something like this:

$("body").append("<div id='popup.dialog'></div>");
dialogDiv = $("#popup.dialog");

But after execution of this d开发者_如何学CialogDiv contains nothing. So is there're way to select newly createn element?


Two things:

1) Dont use periods in your ids

2) you could do it like this a bit better:

var dialogDiv =$('<div id="popup-dialog"></div>').appendTo('body');

At this point you could chain more, or just use the dialogDiv variable.

This stops you from taking a performance hit by selecting an element that you already have access to.


The dot is not valid in the ID. #popup.dialog searches for <div id='popup' class='dialog'>. You should replace it with a dash, like

$("body").append("<div id='popup-dialog'></div>");
dialogDiv = $("#popup-dialog");


I think your problem is the . in the id.

The reason is, that jQuery used the . to match classes, so you are looking for an element with id = popup and class = dialog


You can't have a . in an id.

Try this:

$("body").append("<div id='popup-dialog'></div>");
dialogDiv = $("#popup-dialog");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜