0

由于这个脚本,我不断收到“Unexpected token <”:

<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 名称并重新提交。我仍然收到“意外令牌”<错误...

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

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

2 回答 2

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>
于 2013-09-02T20:12:38.430 回答
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>");
    }                     
});

于 2013-09-02T20:17:30.097 回答