首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自样式标记的意外标记<

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

Stack Overflow用户
提问于 2013-09-02 20:11:03
回答 2查看 1.1K关注 0票数 0

由于这个脚本,我一直得到“意外令牌”:

代码语言: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>

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

编辑:我将=操作符更改为==。我还添加了单引号,但当我向Blogger提交时,我得到了XML错误:“元素的内容必须由格式良好的字符数据或标记组成”,因此我将HTML字符更改为HTML名称并重新提交。我仍然得到“意外的标记”<错误.

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

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-02 20:12:38

至少你得在你的字符串上加上一个单引号.

代码语言: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') {
       // 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>
票数 4
EN

Stack Overflow用户

发布于 2013-09-02 20:17:30

试试这个:

代码语言: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>");
    }                     
});

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18580130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档