简体   繁体   English

在jQuery中获取附加的div ID

[英]get appended div id in jQuery

Is there any way I can get the id (or any other reference to) the element I've just appended using jQuery .append() ? 有什么方法可以获取我刚刚使用jQuery .append()追加的元素的ID(或任何其他引用.append()

I'm receiving an html string with ajax and injecting in a div. 我收到带有ajax的html字符串并注入div。

$.get(URL, args).success(function (response) {

$('#mainDiv').append(response);

});

Now I have to change something to the newly appended div (like a css property). 现在,我必须对新添加的div进行更改(例如css属性)。

Please note! 请注意! These solutions are not accepted, reasons are provided. 不提供这些解决方案,但提供了原因。

  • append to a temporary unique div - reason: the response contains javascript which works only if the injected html is in its environment. 追加到临时唯一的div-原因:响应中包含的javascript仅在注入的html在其环境中时有效。 Calling .append will make this js run and it must be inside of mainDiv. 调用.append将使此js运行,并且它必须在mainDiv中。
  • using a known id for the injected div - reason: the div I'm receiving in response has an id but it could be a random string and, for the moment, I need to assume I don't know it. 为注入的div使用一个已知的id-原因:作为响应,我正在接收的div有一个id,但它可以是随机字符串,目前,我需要假设我不知道。

Perhaps anybody know a secure, reliable and consistent way to access the node I've just created in mainDiv. 也许有人知道一种安全,可靠和一致的方式来访问我刚刚在mainDiv中创建的节点。 If .append always attach at the end, perhaps refering last node is ok: am I wrong? 如果.append总是附加在末尾,则可能引用最后一个节点是可以的:我错了吗?

You can have jQuery object reference like: var j =$(response); 您可以使用jQuery对象参考,例如: var j =$(response); , and then append it : $('#mainDiv').append(j); ,然后附加: $('#mainDiv').append(j); and change its css: j.css("border":"0"); 并更改其CSS: j.css("border":"0");

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

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