Jquery没有加载到头部但是加载在body标签的底部 [英] Jquery not loading in head but loading on bottom of body tag

查看:130
本文介绍了Jquery没有加载到头部但是加载在body标签的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:正如标题所说,只有当我将脚本放在body标签的底部时,jQuery才能正常工作。当我把它们放在头上时它们不起作用。我只使用一些简单的jQuery脚本

Here is my problem: as title says, jQuery is working only when I put the scripts on the bottom of the body tag. When I put them in head they are not working. I am using just some simple jQuery scripts

这是我的< head>

<head>
  <title>P site</title>
  <link rel='stylesheet' href='style.css'/>
  <link rel='stylesheet' type='text/css' href='jquery/css/ui-lightness/jquery-ui-1.10.4.css'/>
  <link rel='stylesheet' type='text/css' href='jquery/time/jquery.ui.timepicker.css'/>
  <style type="text/css">
  <!--
  .pagNumActive {
    color: #000;
    border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
  }
  .paginationNumbers a:link {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
  }
  .paginationNumbers a:visited {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
  }
  .paginationNumbers a:hover {
    color: #000;
    text-decoration: none;
    border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
  }
  .paginationNumbers a:active {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
  }
  -->
</style>
<?php
  require 'connect.inc.php';
  require 'function.php';
?>

< body> 有超过500行,所以我只会告诉你如何在最底层加载jQuery:

The <body> has 500+ rows so I'll just show you how the jQuery is loaded at very bottom:

<script type='text/javascript' src='jquery/js/jquery-1.10.2.js'></script>
<script type='text/javascript' src='jquery/js/jquery-ui-1.10.4.js'></script>
<script type='text/javascript' src='jquery/time/jquery.ui.timepicker.js'></script>
<script type='text/javascript' src='jquery/js/ui.js'></script>

问题在哪里,为什么我不能在<中加载它们? head> tag?

Where is the problem , why can't I load them in <head> tag?

推荐答案

jQuery或JavaScript的工作方式是检测在调用脚本本身时,元素或绑定事件。当文档从上到下加载时,将jQuery放在底部确保在执行JS之前首先加载DOM。另一方面,您可以使用以下技术将jQuery置于顶部:

The way jQuery, or JavaScript for that matter, works is it detects elements or binds events at the time when the script itself is called. As the document is loaded from top to bottom, placing jQuery at the bottom makes sure your DOM is loaded first, before executing JS. On the other hand, you can place jQuery at top, using technique such as this:

$(document).ready(function() {
    console.log('jQuery is now ready to be executed!');

    // Place jQuery code here...
});

这基本上是告诉jQuery,等到DOM被加载,然后才采取行动。

This is basically telling jQuery, wait until DOM is loaded, before acting upon it.

来源: http:// api。 jquery.com/ready/

这篇关于Jquery没有加载到头部但是加载在body标签的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆