比如说var div = document.creatElement("div");
var p = document.creatElement("p");
div.appendChild(p);这样,我不把div添加到html结构中,有办法通过document.getElementByTagName或者jquery选择到这个p么?

解决方案 »

  1.   

    没添加到DOM中。页面不存在这个元素当然获取不到。
      

  2.   

    var div = document.creatElement("div");
    var p = document.creatElement("p");
    p.setAttribute('id','SignP');
    div.appendChild(p);然后document.getElementByID('SignP')不就可以么
      

  3.   

    不可以。
    document.getElementById是Document对象的方法,用于获取文档对象中的元素,但是你的对象并没有添加到文档中,而只是存在于内存中,所以不可以。
      

  4.   

    因为想把一整块HTML结构一下子添加上去,如果一个一个添加的话,网速的慢的用户体验会比较差,好吧,看来没办法了哎