0

在 jquery 代码的一部分中,我在下面有这行代码:

 var newHtml="<span style='color: green'>"+result.msg+"</span>"

现在 jquery 代码显示在该<body>部分中,但问题是我在验证中遇到错误:

文档类型在此处不允许元素“span”

我的问题是有没有办法在保留跨度标签的同时修复它,或者我需要使用替代方法吗?

查看源代码的完整代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title>Edit Assessment Date/Start Time</title>
        <link rel="stylesheet" type="text/css" href="edit_sessionadminStyles.css"/>
        <link rel="stylesheet" type="text/css" href="jquery/ui-lightness/jquery-ui-1.8.16.custom.css"/>
        <script type="text/javascript" src="jquery/jquery-1.7.min.js"></script>
        <script type="text/javascript" src="jquery/jquery-ui-1.8.16.custom.min.js"></script>

        <link rel="stylesheet" type="text/css" href="jquery/ui-lightness/jquery.ui.timepicker.css"/>
        <link rel="stylesheet" type="text/css" href="jquery/ui-lightness/jquery-ui-1.8.14.custom.css"/>
        <link rel="stylesheet" type="text/css" href="jquery/ui-lightness/jquery-ui-timepicker-addon.css"/>
        <script type="text/javascript" src="jquery/jquery.ui.timepicker.js"></script>
        <script type="text/javascript" src="jquery/jquery-ui-timepicker-addon.js"></script>

        </head>
        <body>

    <script type="text/javascript">


     function submitform() {    

        $.ajax({
            type: "POST",
            url: "updatedatetime.php",
            data: $('#updateForm').serialize(),
            dataType:'json',  //get response as json
            success: function(result){
                        if(result.errorflag){

           //do your stuff on getting error message
          var newHtml="<span class='red'>"+result.msg+"</span>"; 
          $("#targetdiv").html(newHtml);  //i am displaying the error msg here

        }else{
           //you got success message

           var newHtml="<span class='green'>"+result.msg+"</span>"; 
                $("#targetdiv").html(newHtml);
                //Get and store the new date and time.
                    var newDate = jQuery("#newDate").val();
                    var newTime = jQuery("#newTime").val();

                    //Set your current date and time to your new date and time.
                    jQuery("#currentDate").val(newDate);
                    jQuery("#currentTime").val(newTime);

                    //Find the currently selected session and update it.
                    var selectedOption = jQuery("#sessionsDrop option:selected");
                    var label = selectedOption.text().split(" - ");
                    selectedOption.text(label[0] + " - " + newDate + " - " + newTime);

                    //Clear the new date and time fields.
                    jQuery("#newDate").val("");
                    jQuery("#newTime").val("");

                    $('#targetdiv').show();
            }
        }
      });        
    }



    $('body').on('click', '#updateSubmit', showConfirm);       


    </script>   

   <noscript style='color: red'><img src="Images/warning-2.fw.png" alt="Javascript Warning" id="warningImage" name="warningSymbol"/> In order to use this application without any problems, you must have javascript enabled</noscript>
Please Login to Access this Page | <a href='./adminlogin.php'>Login</a>        
    </body>
</html>
4

5 回答 5

3

场景 1:当 span 在<Head>标签中时

你必须CDATA像下面这样使用。

<script type="text/javascript">
//<![CDATA[
...code...
//]]>
</script>

检查更多信息脚本标记中何时需要 CDATA 部分?

场景 2:当 span 在<Body>标签中时

<body>
   <div></div>

<script>
   $("div").html("<span class='red'>Hello <b>Again</b></span>");
</script>

</body>

编辑

errorflag你的问题可能是这样的:你必须把msg

您的代码(错误)

if(result.errorflag){

           //do your stuff on getting error message
          var newHtml="<span class='red'>"+result.msg+"</span>"; 
          $("#targetdiv").html(newHtml);  //i am displaying the error msg here

更正一个:

 if(result.errorflag){

               //do your stuff on getting error message
              var newHtml="<span class='red'>"+result.errorflag+"</span>"; 
              $("#targetdiv").html(newHtml);  //i am displaying the error msg here

我希望这能帮到您。

于 2013-01-01T10:12:36.727 回答
0

不,没有。没有显示,<head>所以像 a 这样的视觉元素在<span>那里没有意义。

于 2013-01-01T10:12:46.040 回答
0

嘿使用这个代码它会帮助你

var newHtml="<span class='red'>"+result.msg+"<//span>"; 
于 2013-01-01T10:12:55.073 回答
0

span是一个内联元素,需要一个周围的块元素。只是尝试使用 adiv而不是你的跨度。这应该可以解决验证错误。

于 2013-01-01T11:11:03.950 回答
0

根据 XHTML 规则,script元素内容被解析为 HTML 标记,并且script元素不能包含任何标记。有多种解决方法,包括切换到 HTML 4.01(或 HTML5)。如果您继续使用 XHTML 1.0,那么规范中的建议是:“如果您的脚本使用 <,请使用外部脚本”。也就是说,将script包含脚本代码替换为link引用外部 .js 文件中编写的代码的元素。

于 2013-01-01T11:32:26.603 回答