加载HTML后立即加载jQuery(不是其他Javascript或CSS)

加载HTML后立即加载jQuery(不是其他Javascript或CSS),javascript,jquery,Javascript,Jquery,我希望在加载HTML后立即显示箭头(无需等待其他插件的JavaScript加载)。因此我尝试了以下方法: <script type="text/javascript"> function PreLoad() { $('.page-template-front-page-2-php #header .container').append('<div id="top-line-arrow-home"></div>'); $('.post-typ

我希望在加载HTML后立即显示箭头(无需等待其他插件的JavaScript加载)。因此我尝试了以下方法:

<script type="text/javascript">
  function PreLoad() {
    $('.page-template-front-page-2-php #header .container').append('<div id="top-line-arrow-home"></div>');
    $('.post-type-archive-games #menu-item-402').append('<div id="top-line-arrow"></div>');
    $('.post-type-archive-news #menu-item-420').append('<div id="top-line-arrow"></div>');
    $('.post-type-archive-jobs #menu-item-412').append('<div id="top-line-arrow"></div>');
    $('.page-template-about-page-php #menu-item-19').append('<div id="top-line-arrow"></div>');
    $('.page-template-contact-page-php #menu-item-22').append('<div id="top-line-arrow"></div>');
  }
</script>

<body <?php body_class(); ?> onload="javascript:PreLoad();">

函数预加载(){
$('.page-template-front-page-2-php#header.container').append('');
$('.post-type存档游戏#menu-item-402')。追加('');
$('.post type archive news#menu-item-420')。追加('');
$('.post-type归档作业#menu-item-412')。追加('');
$('.page模板关于页面php#menu-item-19')。追加('');
$('.页面模板联系页面php#菜单项-22')。附加('');
}

有几种替代方法,可能是您的函数
PreLoad()
需要一些时间来执行,这让其他js代码更早地执行,因为我认为没有任何条件会阻止其他代码在您的函数完成执行后执行

将其他javascript代码放在主体部分,将
PreLoad()
放在头部部分也不会有多大帮助

您可以尝试的另一种方法是将您的
PreLoad()
放在document ready事件中,并将其他javascript代码移到
$(window).load()
。因为
$(window).load()
在document ready事件之后执行一小部分时间

另一种方法是使用


希望这对您有所帮助。

使用external.js文件,并在css lik之前参考它和头部的jquery,以确保首先加载它

<head>
  <title>
  </title>
  <script type="text/javascript" src="js/jQuery-v2.js"></script>    
 <script type="text/javascript" src="js/external.js"></script>
 <link rel="stylesheet" type="text/css" href="css/style1.css" />
</head>


如果您使用WordPress,您应该以正确的方式加载脚本,使用
wp\u enqueue\u script
可以定义依赖项和顺序,也可以在页眉或页脚中加载。与样式和
wp\u enqueue\u样式
相同。也许可以看看我的书中这是个坏主意,如果您试图添加clas呢动态加载DOM元素或在js代码中覆盖样式,您可能会得到意外的结果。我知道,他甚至想在css之前加载它,这样他就可以添加其他任何类似于您在另一个.js文件中提到的语句,并在结束body标记之前适当地链接它
<head>
  <title>
  </title>
  <script type="text/javascript" src="js/jQuery-v2.js"></script>    
 <script type="text/javascript" src="js/external.js"></script>
 <link rel="stylesheet" type="text/css" href="css/style1.css" />
</head>