$ .append();不是从外部JavaScript运行

时间:2012-07-12 21:10:57

标签: jquery css

我想在顶部添加一个导航栏,因为我会有很多单独的html文件,我想将导航保存在一个文件中,所以如果我在所有页面中进行更改。如果我在页面中输入代码本身就可以正常工作,但是当它在外部文件中时它会提供不同的视图。

Jquery片段是:

var snippet =   '   <div id="topMainNav">' +
    '   <ul>' +
    '       <li class="mainNav">' +
    '           <a href="" class="navlink">Find a Doctor</a>' +
    '       </li>' +
    '       <li class="mainNav">' +
    '           <a href="" class="navlink">Why Interfaith</a>' +
            '               <div class="dropdown" id="dropdown_one">' +
    '               <div class="subMainNav" style="padding: 10px;">History & Mission</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Executive Director</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Locations & Directions</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Service Centers</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Career Opportunities</div>' +
    '               <div class="subMainNav" style="padding: 10px;">News & Events</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Parking</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Places To Stay</div>' +
    '           </div>' +
    '       </li>' +
    '       <li class="mainNav">' +
    '           <a href="" class="navlink">Medical Services</a>' +
    '           <div class="dropdown" id="dropdown_two">' +
    '               <div class="subMainNav" style="padding: 10px;"><a href="#">Behavioral Health</a></div>' +
    '               <div class="subMainNav" style="padding: 10px;">Clinical Laboratory</div>' +          
    '               <div class="subMainNav" style="padding: 10px;">Dentistry</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Emergency</div>' +      
    '               <div class="subMainNav" style="padding: 10px;">Gynecology</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Medicine</div>' +         
    '               <div class="subMainNav" style="padding: 10px;">Pastoral</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Pediatrics</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Physical Medicine & Rehab</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Radiology</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Surgery</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Other Services</div>' +
    '           </div>' +
    '       </li>' +
    '       <li class="mainNav">' +
    '           <a href="" class="navlink">Medical Trainings</a>' +
    '           <div class="dropdown" id="dropdown_three">' +
    '               <div class="subMainNav" style="padding: 10px;">Medical Training</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Behavioral Health</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Predoctoral Externship</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Podiatric Residency</div>' +          
    '               <div class="subMainNav" style="padding: 10px;">Dental Residency</div>' +
    '               <div class="subMainNav" style="padding: 10px;">Pulmonary Residency</div>' +
    '           </div>' +
    '       </li>' +
    '       <li class="mainNav">' +
    '           <a href="" class="navlink">For Patients & Visitors</a>' +
    '           <div class="dropdown" id="dropdown_four">' +
    '               <p><a href="#">This is a Link</a></p>' +
    '               <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin blandit sodales justo, id fringilla eros dapibus vitae. Morbi molestie enim diam, a vulputate neque. Morbi sit amet nunc.</p>' +
    '           </div>' +
    '       </li>' +
    '       <li class="mainNav">' +
    '           <a href="" class="navlink">Contact Us</a>' +
    '       </li>' +
    '   </ul>' +
    '</div>';

$(function(){$('.writeNav').append (snippet);});

在我的HTML页面上,我有以下内容:

calling the external jscript file:
<script type="text/javascript" src="theScriptsStyles/miscScripts.js"></script>

并且在html的正文中我有这个:

<div class="writeNav"></div>

它应该做的是,有一个下拉菜单,所以当我把鼠标放在它上面时,它有一个无法正常工作的下拉菜单。任何人都可以告诉我我做错了什么。

由于

1 个答案:

答案 0 :(得分:0)

我认为,即使在创建div之前,您的脚本也会被加载。这就是为什么它无法找到该元素的元素./检查是否是这种情况