javascript没有在页面加载时执行? [英] javascript not executing on page load?

查看:83
本文介绍了javascript没有在页面加载时执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的HTML中:

<head>
<link href="/prop-view.css" media="screen" rel="stylesheet" type="text/css">
        <script src="/javascripts/jquery-1.3.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            var $j = jQuery.noConflict();
        </script>
        <script src="/prop-view.js" type="text/javascript"></script>

$j("<div class='dk-logo'></div>").appendTo("body");

检查员没有显示任何错误。但图像没有显示出来。但是,当我将该行复制并粘贴到控制台时。所以...我的猜测是在文档完全加载之前js文件正在运行?

The inspector shows no errors. But the image doesn't show up. It does, however, when I copy and paste that line into the console. So... my guess is the js file is running before the document is fully loaded?

推荐答案

确保DOM已加载首先这样做:

Make sure the DOM is loaded first by doing this:

$j(function() {
    $j("<div class='dk-logo'></div>").appendTo("body");
});

这是jQuery的快捷方式 ready()功能。在< body> 标记完全加载之前,不会执行内部代码。

This is a shortcut for jQuery's ready() function. The code inside is not executed until the <body> tag is fully loaded.

因为你放置了在 < body> 标记之前的代码,还没有< body> 要追加的。

Because you were placing the code before the <body> tag, there was no <body> yet to which to append.

这篇关于javascript没有在页面加载时执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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