从.js文件运行时,document.getElementById(" main")返回null,在控制台中正常工作

时间:2015-06-16 00:00:10

标签: javascript html null

所以,标题总结得很好。我正在敲打这个 我知道空错误通常会由于时间错误而发生,因为我要求它在加载之前在文档中找到某些东西。但是,在我的JS代码中,我能够无问题地获取ElementsByTagName。此外,js函数的整个级联从window.onload开始,因此文档准备就绪,尽可能接近我所知道的。

在.html文件中证明:

<section id="main">

     <header>
        <a href="#">Enter Site</a>
     </header>

     <nav class="links">
        <ul>
           <li><a href="#">Galleries</a></li>
           <li><a href="#">Video</a></li>
           <li><a href="#">Albums</a></li>
           <li><a href="#">Pricing</a></li>
           <li><a href="#">Testimonials</a></li>
           <li><a href="#">Wedding Planner</a></li>
           <li><a href="#">Shopping Cart</a></li>
        </ul>
     </nav>

     <address>
        New Accents Photography &#9830;
        14 Mountain View Road &#9830;
        Ashland, OR  97521 &#9830;
        (541) 555-6002 
     </address>

     <article>
       <p>New Accents is photography studio specializing in wedding 
          portraiture; serving the
          needs of Southern Oregon and Northern California.
       </p>
       <p>We'll capture your wedding in whatever fashion you wish:
          from romantic to zany and all the styles in between.
       </p>
     </article>

     <footer>
        <span class="left">New Accents Photography &copy; 2015 (US)</span>
        <span class="right">
           <a href="#">About</a>
           <a href="#">Terms</a>
           <a href="#">Help</a>
        </span>
     </footer>

  </section>

来自.js文件:

function displayThumbs(){
var figBox = document.createElement("figure");
figBox.setAttribute("id", "thumbnails");
for(var i = 0; i < allSheets.length; i++)
{
    var sheetImg = document.createElement("img");
    var imgName = allSheets[i].title + "_small.png";
    sheetImg.setAttribute("src", imgName);
    sheetImg.title = allSheets[i].title;
    sheetImg.onclick = showSheet();
    figBox.appendChild(sheetImg);
}
var thisMain = document.getElementById("main");
thisMain.appendChild(figBox);
}

0 个答案:

没有答案