English 中文(简体)
如何从动态创造的因素中取回?
原标题:How to retrieve offset of dynamically created elements?

我正在积极利用“javascipt”补充内容:

$( .marker ).append( <div class="container" id="id  + counter +  " ><input type="text" name="textbox" id="textbox  + counter +  " value="" ></div> );
counter++; 

而且,我需要对这些新增内容持立场(第x和 y类)。 用户可以添加和删除这些内容。 之后,应当予以挽救,我需要最后一份要素职位清单。

我曾尝试使用“ j”(<代码>offset(>>)功能,但仅针对没有动态增加的内容。

是否可能重新计算这些有活力的新增内容? 如果是,那么如何?

最佳回答

不知道你是否重新解释以下例子?

http://jsfiddle.net/johncmolyneux/zXSuh/

Just open console and run it.

页: 1

$(".container").each(function() {
    console.log($(this).offset().top);
});
问题回答
var div = $("<div>", {
    "class": "container",
    id: "id" + counter
}).append($("<input>", {
    type: "text",
    name: "textbox",
    id: "textbox" + counter
}));

$(".marker").append(div);

var offset = div.offset();
console.log(offset.left, offset.top);

counter++;

您是否试图将一个变数分配给像这样具有活力的因素:

var el = $( <div class="container" id="id  + counter +  " ><input type="text" name="textbox" id="textbox  + counter +  " value="" /></div> );

$( .marker ).append(el);
counter++; 

那么,你就应当能够使用得力。

el.offset();




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签