JS& jQuery无法检测到html元素,并说它们是未定义的 [英] JS & jQuery can't detect html elements, and say's they are undefined

查看:93
本文介绍了JS& jQuery无法检测到html元素,并说它们是未定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JS和 jQuery 的新手,我正在尝试制作使用它们的字幕播放器。不幸的是,我陷入了很早的阶段。

I'm pretty new to JS and jQuery, and i'm trying to make a subtitles player using them. Unfortunately, i'm stuck in a very early stage.

当我试图选择一些 HTML 元素时通过一个.js文件,它就像找不到我要求的元素一样,没有任何反应。如果我尝试提醒元素的值或HTML,它会提醒 undefined

When I'm trying to select some HTML elements through a .js file, it acts like it can't locate the element I'm asking for, and nothing happens. If I try to alert the value or the HTML of the elements, it alerts undefined.

所以这是代码:

HTML

  <!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <script src="jquery-2.1.3.min.js"></script>
        <script src="script.js"></script>
        <style type="text/css">
            body{
                margin: 0px;
                padding: 0px;           
            }           
            #wrapper{
                width: 150px;
                text-align: center;
                background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,#75bdd1), color-stop(14%,#75bdd1), color-stop(100%,#2294b3));
                padding: 10px 2px;
            }
            h3{
                font-family: Arial, Helvetica, sans-serif;
            }
            img{
                width: 50px;
                margin: 0 auto;     
            }
            input{
            display: none;          
            }
        </style>


    </head>
    <body>
        <div id="wrapper">
            <h3>Select subtitles file</h3>
                <img src="browse.png" alt="browse">
        </div>
        <input type="file" accept=".srt" id="file">
    </body>
</html>

script.js

$("div").click(function () {
    console.log('loaded');
});

谢谢。

推荐答案

因为你的脚本标签 上面是定义它所作用的元素的HTML,所以它找不到它们,因为它们是在该代码运行时不存在。以下是页面中发生的事情的顺序:

Because your script tag is above the HTML defining the elements that it acts on, it can't find them because they don't exist as of when that code runs. Here's the order in which things are happening in your page:


  1. html head 元素已创建

  2. 已创建元素,并注明其内容解析器

  3. 创建了jQuery的脚本元素

  4. 解析器停止并等待加载jQuery文件

  5. 加载后,执行jQuery文件

  6. 解析继续

  7. 创建代码的脚本元素

  8. 解析器停止并等待要加载您的文件

  9. 加载后,您的脚本代码就会运行—并且没有找到任何元素,因为没有 div 元素

  10. 解析继续

  11. 浏览器完成解析和构建页面,其中包括创建您尝试在脚本中访问的元素

  1. The html and head elements are created
  2. The meta element is created and its content noted by the parser
  3. The script element for jQuery is created
  4. The parser stops and waits for the jQuery file to load
  5. Once loaded, the jQuery file is executed
  6. Parsing continues
  7. The script element for your code is created
  8. The parser stops and waits for your file to load
  9. Once loaded, your script code is run — and doesn't find any elements, because there are no div elements yet
  10. Parsing continues
  11. The browser finishes parsing and building the page, which includes creating the elements you're trying to access in your script

如何使用纠正它:


  1. 脚本元素移到最后,就在结束< / body> 标记之前,所以在代码运行之前存在所有元素。除非有充分的理由不这样做,这通常是最好的解决方案。

  1. Move the script elements to the very end, just before the closing </body> tag, so all of the elements exist before your code runs. Barring a good reason not to do this, this is usually the best solution.

使用jQuery的 ready 功能

Use jQuery's ready feature.

使用< a href =http://www.w3.org/TR/html5/scripting-1.html#attr-script-async =nofollow> 推迟脚本元素上的属性,但要注意并非所有浏览器都支持它。

Use the defer attribute on the script element, but beware that not all browsers support it yet.

但是,如果您控制脚本元素的去向,#1通常是您最好的选择。

But again, if you control where the script elements go, #1 is usually your best bet.

这篇关于JS&amp; jQuery无法检测到html元素,并说它们是未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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