简体   繁体   English

如何将 document.getElementById(id) 元素转换为 jquery object?

[英]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.

相关问题 jQuery(“#”+ id)vs document.getElementById(“”+ id) - jQuery(“#”+id) Vs document.getElementById(“”+id) 如何在jquery插件中使用document.getElementById,以便它可以获取当前正在使用该插件的对象的ID - how to use document.getElementById in jquery plug in so that it can take the id of the object which is using the plugin currently JQuery select 按 ID 与 document.GetElementByID - JQuery select by ID vs document.GetElementByID 如何访问从jQuery中的document.getelementbyId()获得的JavaScript对象? - How to access a JavaScript object got from document.getelementbyId() in jQuery? 如何使用document.getElementById(“ mydiv”)编辑具有相同ID的多个div - How do i use document.getElementById(“mydiv”) to edit multiple divs with the same ID 我如何让 document.getElementById(&#39;id&#39;).value 在 Javascript 中返回一个数字而不是一个字符串 - How do I make document.getElementById('id').value return a number not a string in Javascript 如何使用 document.getElementById(“abc”).innerHTML DOM 以字符串格式显示 JavaScript object 的内容? - How do I display the content of a JavaScript object in a string format using document.getElementById(“abc”).innerHTML DOM? 如何使用document.getElementById打印id get? - How to print an id get with document.getElementById? 如何使用模态输入中的document.getElementById? - How do I use document.getElementById from an input in a modal? 如何在单独的 js 文件中使用 document.getElementById()? - How do I use document.getElementById() in a separate js file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM