简体   繁体   English

内联javascript起作用,但外部文件不起作用

[英]inline javascript works but external file does not

The problem is pretty straight forward. 问题很简单。 The following code works perfectly when included inside script tags in my HTML document: 将以下代码包含在HTML文档中的脚本标签中时,它们可以完美地工作:

$(document).ready(function ($) {
    'use strict';
    $('#contact-form').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        submitButtons: $('#contact-form-submit-button')
    });
}(window.jQuery));

However, once I try to load the script externally like this, it completely fails to do anything (I can confirm that the resource is getting properly served by the server.): 但是,一旦我尝试像这样从外部加载脚本,它完全无法执行任何操作(我可以确认服务器已正确提供了资源。):

<script type="text/javascript" src="/Users/nick/Documents/coding/sparestub/static_root/contact/js/contact.js"></script>

Does anyone have a clue what's going on here? 有人知道这是怎么回事吗? I'm think I'm going crazy. 我想我快要疯了。

EDIT: I figured it out. 编辑:我想通了。 It turns out the element containing the #contact-form was being loaded asynchronously :( 原来包含#contact-form的元素是异步加载的:(

Put the file on your project file and you could do something like this, 将文件放在项目文件中,您可以执行以下操作,

~/Js/contact.js 〜/ Js / contact.js

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM