具有id的CreateElement? [英] CreateElement with id?

查看:162
本文介绍了具有id的CreateElement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改此代码,以便为此div项目提供ID,但是我在google上找不到任何内容,并且idName不起作用。我读了一些关于追加的内容,但对于看似非常简单的任务来说,这似乎相当复杂,那么还有其他选择吗?谢谢:)

I'm trying to modify this code to also give this div item an ID, however I have not found anything on google, and idName does not work. I read something about append, however it seems pretty complicated for a task that seems pretty simple, so is there an alternative? Thanks :)

g=document.createElement('div'); g.className='tclose'; g.v=0;


推荐答案

你应该使用 .setAttribute() 方法:

You should use the .setAttribute() method:

g = document.createElement('div');
g.setAttribute("id", "Div1");

这篇关于具有id的CreateElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆