【问题标题】:How to disable CSS on a specific page?如何在特定页面上禁用 CSS?
【发布时间】:2015-11-03 19:38:32
【问题描述】:

我在许多页面中使用了 3 个样式表(style1style2style3)。

  • style1 我的网页标题,
  • style2 我的内容和
  • style3 用于页脚

现在我想在我的主页上应用一个新的stylesheet,并为主页禁用style2

我为不同的元素添加了新的类,但在某些地方它仍然检测到我以前的stylesheets。如何在特定页面上禁用 CSS?

【问题讨论】:

  • 我们需要查看您遇到问题的代码。
  • 这里是 web design.earnandearn.com,我只想禁用 core.css 的主页
  • 从您的网站看来,这是一个简单的link 元素。如果是这样,只需删除链接元素并将其替换为新样式表。
  • 如果我删除链接所有其他页面会打扰:(

标签: html css


【解决方案1】:

由于您没有包含代码以查看您的操作方式,并且考虑到您将标头作为包含文件:

使用 PHP:

在您的主页中包含标题之前:

<?php $homepage = 1; ?>

//include your header//

在您引用 CSS 的标题中:

<link href="css/style1.css" rel="stylesheet" type="text/css" />

<link href="css/style3.css" rel="stylesheet" type="text/css" />

<link href="<?php if($homepage == 1){ echo "css/Home-style.css";}else{ echo "css/style2.css";}?>" rel="stylesheet" type="text/css" />

您的文件应该在 PHP 中才能正常工作。 将“Home-style.css”更改为新的主页 CSS 文件。

【讨论】:

    【解决方案2】:

    检查页面然后抓取 dom 样式表数组对象并禁用任何你想要的。

    if(window.location.href.indexOf("<name in page url >") > -1) {
    
    //Next line will disable the 2nd stylsheet in the document . 
    
     document.styleSheets[1].disabled = true;
    
    }
    

    另外,您可以使用以下命令查看 dom 中的所有样式表:

    console.log(document.styleSheets) 
    

    这应该为您提供要删除的任何样式表的适当索引。

    【讨论】:

      【解决方案3】:

      您必须使用jQuery 来禁用stylesheet 之类的:

      <script>
      $(function () {
        if (location.pathname.indexOf('/pagename') >= 0) {
          $('link[href*="/media/css/csspagename.css"]').prop('disable', true);
        }
      });
      </script>
      

      【讨论】:

      • 我已经尝试过将它粘贴到我的主页上,但没有禁用我所需的工作表
      猜你喜欢
      • 2019-08-01
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多