意外的标记<来自样式标记

Unexpected token < From a Style Tag

本文关键字:样式 意外 lt      更新时间:2023-09-26

因为这个脚本,我一直得到"意外的令牌<":

<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标记,我试着使用实体名而不是字符,我确保文档.write中没有空白,等等。为什么它不起作用?我认为document.write支持HTML实体?

EDIT:我将=运算符更改为==。我还添加了单引号,但当我提交给Blogger时,我得到了XML错误:"元素的内容必须由格式良好的字符数据或标记组成",所以我将HTML字符改为HTML名称并重新提交。我仍然得到"意外代币"<错误

UPDATE我已经更新了脚本,但仍然得到了完全相同的错误:

<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>

至少您必须在字符串周围添加一个引号。。。

<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>

试试这个:

<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>");
    }                     
});