[英]How do I turn a document.getElementById(id) element into a jquery object?
I'm actually surprised this question is not asked before:我真的很惊讶以前没有问过这个问题:
I generate elements with:我生成元素:
document.getElementById(id).innerHTML = (some string with id's in it);
for example.例如。
And I want to use jQuery (to update the CSS of the children of elements).我想使用 jQuery (更新元素子元素的 CSS )。
As those elements are created dynamically a simple:由于这些元素是动态创建的,因此很简单:
$("#id")
doesn't work.不起作用。
(It returns n.fn.init[0]
objects). (它返回
n.fn.init[0]
对象)。
How do I get a jQuery object from a HTML DOM object?如何从 HTML DOM ZA8CFDE6331BD59EB2AC96F8911C4B6666Z 获得 jQuery object?
If you are using innerHTML to generate DOM elements you might need to access the from parent children and then initialize the jQuery array on it (with $)如果您使用 innerHTML 生成 DOM 元素,您可能需要访问父子元素,然后在其上初始化 jQuery 数组(使用 $)
const container=document.getElementById(id);
container.innerHTML = '<div>...</div>'
$(container.children)
You simply do this:你只需这样做:
$(document.getElementById(id))
you can use it as a jquery object:您可以将其用作 jquery object:
$(document.getElementById(id)).children().css("opacity", "" + opacityTotal);
You can update the css of the dynamically added children in the document simply as:您可以在文档中更新动态添加的子项的 css,如下所示:
$(document).find(".classOfYourDynamicallyAddedChildrens").css({"color":"red"});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.