【问题标题】:Jquery/Javascript append doesn't runJquery/Javascript 追加不运行
【发布时间】:2014-03-07 17:19:12
【问题描述】:

我正在测试这个 jQuery 代码,但它没有运行。我打开了控制台(F12),在我粘贴了测试代码后,它完美地编译了它。虽然我删除了 src="http://code.jquery.com/jquery-latest.min.js" 并从 $(document).ready(function(){ 中获取警报然后警报有效,但我明白了来自控制台的消息:“Uncaught ReferenceError: $ is not defined”。我想知道这是为什么?请帮忙?

这是我的测试代码:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">


$(document).ready(function(){    
alert('Hi!');
$("#content").append('<div class=\'toolbarBox toolbarHead\' style=\'background-color: #F8F8F8;border: 1px solid #CCCCCC;margin-bottom: 10px;padding: 5px 0;border-radius: 3px;-moz-border-radius: 3px;-webkit-border-radius: 3px;-o-border-radius: 3px;position: relative;\'><div class=\'pageTitle\'><h3 style = \'font-size: 1.6em;font-weight: normal;line-height: 52px;text-shadow: 0 1px 0 #fff;\'><span id=\'current_obj\' style=\'font-weight: normal;\'><span class=\'breadcrumb item-0\'>My Affiliates<img alt=\'&gt;\' style=\'margin-right:5px\' src=\'../img/admin/separator_breadcrumb.png\'></span><span class=\'breadcrumb item-1\'>Affiliates</span></span></h3></div></div>'); 
$("#content").append("<iframe style=\'width: 100%;height: 100%;\' src=\'http://www.myaffiliates.gr/\'></iframe>");

});

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您的脚本标签不正确

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    // your script
    </script>
    

    脚本元素可以包含src 或正文,但不能同时包含两者。

    Script : src

    此属性指定外部脚本的 URI;这可以是 用作将脚本直接嵌入到 文档。指定了 src 属性的脚本元素不应该 在其标签中嵌入一个脚本。

    【讨论】:

    • 你能解释一下为什么它需要 2 个脚本元素吗?
    • @Sergiti 因为这就是脚本标签的工作方式...如果指定了src 属性,那么开始标签和结束标签之间的内容必须为空 - javascript.crockford.com/script.html
    【解决方案2】:

    试试

       <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        $(document).ready(function () {
            alert('Hi!');
            $("#content").append('<div class=\"toolbarBox toolbarHead\" style=\"background-color: #F8F8F8;border: 1px solid #CCCCCC;margin-bottom: 10px;padding: 5px 0;border-radius: 3px;-moz-border-radius: 3px;-webkit-border-radius: 3px;-o-border-radius: 3px;position: relative;\"><div class=\"pageTitle\"><h3 style =\"font-size: 1.6em;font-weight: normal;line-height: 52px;text-shadow: 0 1px 0 #fff;\"><span id=\"current_obj\" style=\"font-weight: normal;\"><span class=\"breadcrumb item-0\">My Affiliates<img alt=\"&gt;\" style=\"margin-right:5px\" src=\"../img/admin/separator_breadcrumb.png\"></span><span class=\"breadcrumb item-1\">Affiliates</span></span></h3></div></div>');
            $("#content").append("<iframe style=\"width: 100%;height: 100%;\" src=\"http://www.myaffiliates.gr/\"></iframe>");
    
        });
    </script>
    

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 2020-07-15
      • 2018-01-06
      • 2011-08-31
      • 1970-01-01
      • 2015-08-03
      • 2021-12-20
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多