【问题标题】:Script error in JQueryJQuery 中的脚本错误
【发布时间】:2011-12-16 12:51:36
【问题描述】:

我在 Jquery 中遇到脚本错误。请建议如何解决这个问题。显示的脚本错误信息是"HTML解析错误:无法在子元素关闭前修改父容器元素"。 code:0 line:0 char:0。我的 Jquery 代码是:

      <script type="text/javascript">

     (function($) {
     var search=window.location.search.substring(1);
     var page=search.split("=");
     var location=window.location.toString();
     var url=location.split('?')[0];         

     if(page[1]=='custDetails'){

       $(document).ready(function(){
       $('#message').dialog('open');
       $(document).ready(function(){
       $('#pop').click(function(){
      $('#message').dialog('open');
       return false;
         });
       });
      });
     }   // end of if      
        else {
        $(document).ready(function(){
        $('#pop').click(function(){
        $('#message').dialog('open');
        return false;
          });
        });

        }  // end of else

        $('#message').dialog({
        width:200,
        autoOpen:false,
        buttons:{ 
           Close:function() {
       $ (this.dialog('close');
       $ ('#message').replaceWith('url');
                 }
              } 
          });

       $('#page').click((function(event){            
         window.print();
          });

        })  ($);
      </script>

当我删除 $('#message').dialog({}); 组件它不会引发脚本错误。请告诉我原因。

【问题讨论】:

  • ...并包含错误消息。
  • 为什么有这么多$(document).ready 事件处理程序?一个就够了。
  • 如果你格式化你的代码,错误会更容易被发现。
  • 我投了反对票,只是因为从这样一个基本的角度来看,这做得太糟糕了——我知道这不完全符合本网站的精神,我从不这样做,但哎呀......跨度>
  • @Jørgen ,我收到此错误消息。 “HTML 解析错误:在子元素关闭之前无法修改父容器元素”。代码:0 行:0 字符:0

标签: jquery


【解决方案1】:

充满了语法错误...

$('#message').dialog($  // << ERROR 1 should be {
        width:200; // << ERROR 2 the ; should be ,
        autoOpen:false; // << ERROR 3 the ; should be ,
        buttons.{close:function() { // << ERROR 4 the . should be :
       $ (this.dialog('close'); // << ERROR 5 the $(this should be $(this).
          }}
         });

应该是这样的

    $('#message').dialog({
        width:200,
        autoOpen:false,
        buttons: { close:function() {
                          $(this).dialog('close');
                         }
                 }
        });

【讨论】:

    【解决方案2】:

    这里有几个语法错误:

    $('#message').dialog($
        width: 200;
        autoOpen: false;
        buttons. {
        close: function() {
            $(this.dialog('close');
            }
        }
    });
    $('#page').click((function(event) {
        window.print();
    });
    

    试试这个:

    $('#message').dialog({
        width: 200,
        autoOpen: false,
        buttons: {
            close: function() {
                $(this.dialog('close');
                }
            }
        });
    });
    
    $('#page').click(function(event) {
        window.print();
    });
    

    【讨论】:

    • 感谢epignosisx,在我不删除之前它仍然显示脚本错误
    • @user1057697 你还有一个语法错误。查找 $(this.dialog('close'); 应该是 $(this).dialog('close'); 你应该尝试jslint.com。它会识别出这种错误。
    【解决方案3】:

    如果出现错误,你确定你包含了 jQueryui js 文件吗?类似于

    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
    

    在你包含你的 jQuery js 文件之后?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多