0

jqueryコードの一部に、次のコード行があります。

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

これで、jqueryコードが<body>セクションに表示されますが、問題は、検証で次のようなエラーが発生することです。

ドキュメントタイプでは、ここで要素「スパン」を使用できません

私の質問は、スパンタグを保持しながらそれを修正する方法はありますか、それとも別の方法を使用する必要がありますか?

ビューソースからの完全なコード:

<!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 : スパンが<Head>タグにある場合

以下のように使用する必要がありCDATAます。

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

詳細情報を確認する script タグ内で CDATA セクションが必要になるのはいつですか?

シナリオ 2: スパンが<Body>タグにある場合

<body>
   <div></div>

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

</body>

編集

あなたの問題はこれかもしれません:あなたは入れなければなりませerrorflagmsg

あなたのコード(間違っています)

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

XHTML ルールにより、script要素のコンテンツは HTML タグ用に解析され、script要素にタグを含めることはできません。これには、HTML 4.01 (または HTML5) への切り替えなど、さまざまな方法があります。XHTML 1.0 を使用し続ける場合、仕様の推奨事項は次のとおりです。「スクリプトで < を使用する場合は外部スクリプトを使用する」。つまり、script含まれているスクリプト コードをlink、外部の .js ファイルに記述されたそのコードを参照する要素に置き換えます。

于 2013-01-01T11:32:26.603 に答える
0

spanはインライン要素であり、周囲のブロック要素が必要です。divスパンの代わりにa を使用してみてください。これにより、検証エラーが修正されます。

于 2013-01-01T11:11:03.950 に答える
0

いいえ、ありません。は<head>表示されないため、 のような視覚要素<span>は意味を持ちません。

于 2013-01-01T10:12:46.040 に答える
0

ちょっとこのコードを使ってください

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