简体   繁体   English

放在head标签中的jQuery代码不起作用

[英]Jquery Code not working when placed in head tag

i have some jquery code which doesn't work when placed inside the head tag but works when placed after the body tag. 我有一些jquery代码,当放在head标签内时不起作用,但是当放在body标签后时起作用。 Even though it is inside document.ready() it isn't working. 即使它在document.ready()内部,也不起作用。

Here is my code: 这是我的代码:

$(document).ready(function() {
    for (i = 1950; i <= new Date().getFullYear(); i++) {
        $('#from').append($('<option />').val(i).html(i));
        $('#to').append($('<option />').val(i).html(i));
    }
})

Here is my Structure 这是我的结构

 < head>

     <script type="text/javascript" src="scripts/jquery/js/jquery-1.4.2.min.js"></script>

   <script type="text/javascript" src="scripts/jquery/js/jquery-ui-1.8.17.custom.min.js"></script>

    <script type="text/javascript" src="/easyoffice/js/excelExport.js"></script>
     <script type="text/javascript" src="/easyoffice/js/tinytable.js"></script>

   <script type="text/javascript">
$(document).ready(function() {
    for (i = 1950; i <= new Date().getFullYear(); i++) {
        $('#from').append($('<option />').val(i).html(i));
        $('#to').append($('<option />').val(i).html(i));

    }

})
      function checkyear() {
    var from = $('select[id=to]').val();
    var to = $('select[id=from]').val();
    if (from == 'Select' || to == 'Select') {
        alert(" Please Select an Year  ")
        return false;
    } else if (from <= to) {

        alert(" Please Select an Year greater than From Year ")
        $("select#to").prop('selectedIndex', 0);

        return false;
    } else {
        return true;
    }
}

   </script>
   </head>

please check sequence of where you have added yout jquery file in head tag? 请检查在head标签中添加了jquery文件的位置的顺序? it should be 它应该是

<script src="your_jquery_library_file.js" type="text/javascript"></script>
//Then
<script type="text/javascript">
   $(document).ready(function() {
          for (i = 1950; i <= new Date().getFullYear(); i++) {
        $('#from').append($('<option />').val(i).html(i));
        $('#to').append($('<option />').val(i).html(i));

        }
      })
</script>

$(document).ready() simply ensures that all DOM elements are loaded before your javascript is loaded. $(document).ready()只是确保在加载JavaScript之前先加载所有DOM元素。

When it doesn't matter 没关系的时候

Without waiting for this event to fire, you may end up manipulating DOM elements or styles that are yet to exist on the page. 在不等待此事件触发的情况下,您可能最终要处理页面上尚不存在的DOM元素或样式。 the DOM ready event also allows you more flexibility to run scripts on different parts of the page. DOM ready事件还使您可以更加灵活地在页面的不同部分上运行脚本。 For example: 例如:

    <div id="map"></div>
    <script type="text/javascript">
     document.getElementById('map').style.opacity = 0;
    </script>

will run because the map div has been loaded before the script runs. 将运行,因为在脚本运行之前已加载了map div。 In fact, you can get some pretty good performance improvements by placing your scripts at the bottom of the page. 实际上,通过将脚本放在页面底部,您可以获得一些相当不错的性能改进。

When it does matter 什么时候重要

However, if you are loading your scripts in the element, most of your DOM has not loaded. 但是,如果要在元素中加载脚本,则大多数DOM都没有加载。 This example will not work: 此示例不起作用:

    <script type="text/javascript">
    document.getElementById('map').style.opacity = 0;
    </script>
    <div id="map"></div>

will not, since the map div has not been loaded. 不会,因为尚未加载map div。

It'll all depend on when certain parts of the code are executed. 这完全取决于代码的某些部分何时执行。 If they're in the header, then the elements they are attempting to access might/will* not exist yet. 如果它们在标题中,则它们尝试访问的元素可能/将*不存在。

I figured out what was the problem. 我找出了问题所在。 I put the ready function after the <body> tag and now it is working perfectly. 我将ready函数放在<body>标记之后,现在可以正常使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 放置在head标签中时无法识别样式 - style not recognized when placed in the head tag 放在head标签中时,jQuery代码不起作用 - jQuery code doesn't work when put within head tag 为什么将这段JavaScript代码放在里面时不起作用 <head> 但是当放在底部时可以使用 <body> - Why doesn't this piece of JavaScript code work when placed inside <head> but works when placed at the bottom of <body> 当 label 标签放在输入标签之前时,Css 不起作用? - Css not working when label tag placed before input tag? 将脚本放在head标签内时,为什么HTML5 Canvas无法绘制? - Why HTML5 Canvas does not draw when script is placed inside head tag? 如何将脚本标签加载为 DOM 元素并像放置在头标签中一样运行 - How load script tag as DOM element and run as if it was placed in head tag 将jQuery ready事件放在Rails的HEAD中时,不会在每次加载页面时触发 - jQuery ready event does not fire on every page load when placed in HEAD in Rails 启动 jQuery 脚本在放置在控制台时有效,但在放入站点代码时无效 - Initiating jQuery script works when placed in console but not when placed into site code 在页面加载时将放置在头部的渲染标签注入到身体中 - Inject a render tag placed in the head into body on page load 放在.js单独文件上时jQuery函数不起作用 - jQuery function not working when placed on .js separate file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM