意外的令牌<从样式标签

时间:2013-09-02 20:11:03

标签: javascript

我不断收到“意外的令牌<”因为这个脚本:

<script type='text/javascript'>
  $(document).ready(function(){
    if (window.location.pathname + window.location.search = '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
      document.write (<style type="text/css">#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }</style>);
    }
  });
</script>

我不明白为什么会抛出这个错误。我现在已经研究了大约2个小时。我尝试添加CDATA标签,我尝试使用实体名称而不是字符,我确保在document.write等中没有空格,等等。为什么它不起作用?我以为document.write支持HTML实体?

编辑:我将=运算符更改为==。我还添加了单引号,但是当我提交给Blogger时,我收到了XML错误:“元素的内容必须包含格式良好的字符数据或标记”,因此我将HTML字符更改为HTML名称并重新提交。我仍然得到“意外的令牌”&lt;错误...

更新我已将脚本更新为此,但仍然会收到完全相同的错误:

<script type='text/javascript'>
  <![CDATA[
    $(document).ready(function(){
      if ((window.location.pathname + window.location.search) === '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
         document.write ('<style type="text/css">#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }</style>');
      }
    });
  ]]>
</script>

2 个答案:

答案 0 :(得分:4)

至少你必须在你的字符串周围添加一个引用...

<script type='text/javascript'>
  $(document).ready(function () {
    if ((window.location.pathname + window.location.search) === '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
       // add the style to your head
       $('head').append(String.fromCharCode(60) + 'style type="text/css">#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }' + String.fromCharCode(60) + '/style>');
       // or decide to individually show the divs with jquery selectors
       $('div#HTML25').css('display', 'block');

    }
  });

</script>

答案 1 :(得分:0)

试试这个:

<script type='text/javascript'>
    $(document).ready(function(){
        if (window.location.pathname + window.location.search == '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {

            document.write("<style type='text/css'>#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }</style>");
    }                     
});