简体   繁体   English

通过jquery动态创建和选择html元素

[英]Dynamic creation and selection of html elements via jquery

How can I access the dynamically created html element by its Id on very next line of this following code? 在下面的代码的下一行,如何通过其ID访问动态创建的html元素?

var line = $('<div class="showInGrid" id="removeMeLater">
              <span class="dateP" id="calendar' + ind + '" ></span>
              </div>');

I want to access it like the following line of code. 我想像下面的代码行一样访问它。

$("#calendar"+ind).datepicker({});

However I'm able to link the datepicker with it like the following. 但是,我可以将datepicker与其链接,如下所示。

line.datepicker({});

But obviously it is not giving me the desired result and it gets created every time loop iterates over it. 但是显然,它并没有给我想要的结果,并且每次循环对其进行迭代时都会创建它。

The problem is you have not added the element to the DOM. 问题是您尚未将元素添加到DOM。

jquery selector will search inside the DOM. jQuery选择器将在DOM中搜索。 since the element is not in DOM it will return an empty array. 由于该元素不在DOM中,它将返回一个空数组。

run $("#calendar"+ind).datepicker({}); 运行$("#calendar"+ind).datepicker({}); after appending it to the body 将其附加到身体上之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM