2

私はJavaScriptが初めてです。これは私のコードのフィドルです。

削除するCDATAと、フィドルでは問題なく動作しますが、Eclipse などの XHTML エディターでは問題が発生します。

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

これは私のJavaScriptです:

<![CDATA[
  function test() {
      alert(document.getElementById("divId"));
      var regex = new RegExp('this',"gi");
      document.getElementById("divId").innerHTML
      = document.getElementById("divId").innerHTML.replace(regex, 
            function(matched) 
            {
                return '<span class=\'highlight\'>' + matched + '</span>';
            });
  }
]]>

ここ<div>に問題があります:

<div id="divId">
    This is the text This is the text This is the text This is the text 
    This is the text This is the text This is the the text
</div>

関数を呼び出すことができませんtest()。助言がありますか?

4

2 に答える 2

5

CDATA 行をコメントするだけです:

...
// <![CDATA[
...
// ]]>
于 2013-07-03T10:27:53.027 に答える
3

/* */ で囲む

 /*<![CDATA[*/

            function test(){
        alert(document.getElementById("divId"));
         var regex = new RegExp('this',"gi");
         document.getElementById("divId").innerHTML     
         =document.getElementById("divId").innerHTML.replace(regex, function(matched) 
        {
            return '<span class=\'highlight\'>' + matched + '</span>';
        });


    }

/*]]>*/
于 2013-07-03T10:27:29.440 に答える