仅当 url 为 https://mywebsite.com/my-account/return 时添加 css

Add css only when url is https://mywebsite.com/my-account/return

只有当windowurl为https://mywebsite.com/my-account/return

时,我才想加载以下CSS

CSS:

<style>.shop table.shop_table.my_account_orders>tbody>tr{
    display: table-row !important;}

.shop table.shop_table.my_account_orders>tbody>tr>td{
    display: table-cell !important;}

.shop table.shop_table.my_account_orders>tbody>tr>td:before{
    display: table-row !important;}</style>

您可以将这些显示规则添加到 class,然后根据 window.location 将 class 动态添加到正文。

您的 CSS 规则看起来像这样(注意新的 .mywebsite-com-my-account-return class):

<style>
    .mywebsite-com-my-account-return .shop table.shop_table.my_account_orders>tbody>tr {
        display: table-row !important;
    }

    .mywebsite-com-my-account-return .shop table.shop_table.my_account_orders>tbody>tr>td {
        display: table-cell !important;
    }

    .mywebsite-com-my-account-return .shop table.shop_table.my_account_orders>tbody>tr>td:before {
        display: table-row !important;
    }
</style>

那么您的 Javascript 将如下所示:

<script>
    if (window.location.href === "https://mywebsite.com/my-account/return") {
        document.querySelector("body").classList.add("mywebsite-com-my-account-return")
    }
</script>

如果你想加载样式,试试jquery

<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function(){
      var pageURL = $(location).attr("href");
      console.log(pageURL);
      if( pageURL == 'https://mywebsite.com/my-account/return') {
          $('head').append('<style>.shop table.shop_table.my_account_orders>tbody>tr{\
    display: table-row !important;}\
.shop table.shop_table.my_account_orders>tbody>tr>td{\
    display: table-cell !important;}\
.shop table.shop_table.my_account_orders>tbody>tr>td:before{\
    display: table-row !important;}</style>');
      }
    });
  </script>
</head>
<body>
</body>
</html>