CreateElement with id?

后端 未结 7 1851
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 01:05

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

相关标签:
7条回答
  • 2020-11-28 01:26
    var g = document.createElement('div');
    g.id = 'someId';
    
    0 讨论(0)
  • 2020-11-28 01:30
    var element = document.createElement('tagname');    
    
    element.className= "classname";
    element.id= "id";
    

    try this you want.

    0 讨论(0)
  • 2020-11-28 01:31

    Why not do this with jQuery?

    var newDiv= $('<div/>', { id: 'foo', class: 'tclose'})
    
    0 讨论(0)
  • 2020-11-28 01:32

    I'm not sure if you are trying to set an ID so you can style it in CSS but if that's the case what you can also try:

    var g = document.createElement('div');    
    
    g.className= "g";
    

    and that will name your div so you can target it.

    0 讨论(0)
  • 2020-11-28 01:37

    You should use the .setAttribute() method:

    g = document.createElement('div');
    g.setAttribute("id", "Div1");
    
    0 讨论(0)
  • 2020-11-28 01:48

    You can use g.id = 'desiredId' from your example to set the id of the element you've created.

    0 讨论(0)
提交回复
热议问题