2013-01-01 49 views
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> 
+0

你想用Span在部分做什麼? – ryadavilli

+0

也許將代碼包裝在cdata或註釋中 – mplungjan

+0

發佈html代碼文件 –

回答

3

情形1:跨度是<Head>標籤

你必須使用CDATA像下面。

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

檢查更多信息When is a CDATA section necessary within a script tag?

方案2:當跨度是<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 

我希望這會幫助你。

+0

對不起,這是在身體部分不是頭部分。對不起回合 – user1830984

+0

這仍然導致驗證錯誤 – user1830984

+0

@ user1830984嘗試使用像上述方案2. – Sampath

0

不,沒有。 <head>不顯示,因此像<span>這樣的視覺元素在那裏沒有意義。

+0

對不起,它是在身體部分而不是頭部。對不起回合 – user1830984

0

哎使用此代碼它會幫助你

var newHtml="<span class='red'>"+result.msg+"<//span>"; 
+0

如果我這樣做,這仍然給我一個額外的錯誤 – user1830984

0

span是一個內聯元素,需要周圍塊元素。只是嘗試使用div而不是您的跨度。這應該修復驗證錯誤。

0

通過XHTML規則,script元素內容被解析爲HTML標籤,並且script元素不得包含任何標籤。有很多種方法,包括切換到HTML 4.01(或HTML5)。如果繼續使用XHTML 1.0,那麼規範中的recommendation是:「如果腳本使用<,則使用外部腳本」。即,將包含腳本代碼的script替換爲link元素,該元素引用寫入外部.js文件中的代碼。

相關問題