WordPress网站 – 在满足特定条件时应用CSS

huangapple go评论67阅读模式
英文:

WordPress site - applying CSS when certain criteria are met

问题

这是网站上用于特定预订的

元素 - 在这个示例中,数字7568是旅行ID。

<div class="tourmaster-template-wrapper" id="tourmaster-payment-template-wrapper" data-ajax-url="https://www.domainname.co.uk/wp/wp-admin/admin-ajax.php" data-booking-detail="{&quot;tour-id&quot;:&quot;7568&quot;}">

对于特定的旅行ID,我想要隐藏页面上的另一个

元素,所以是否有一种方法可以在data-booking-detail中包含特定数字时应用CSS到页面上的另一个

元素?

所以如果data-booking-detail包含7568,则隐藏提供立即预订并以后付款的

元素。

英文:

This is the div on the site for a particular booking - in this example the number 7568 is the tour ID.

&lt;div class=&quot;tourmaster-template-wrapper&quot; id=&quot;tourmaster-payment-template-wrapper&quot; data-ajax-url=&quot;https://www.domainname.co.uk/wp/wp-admin/admin-ajax.php&quot; data-booking-detail=&quot;{&amp;quot;tour-id&amp;quot;:&amp;quot;7568&amp;quot;} &gt;

For particular tour ID's I would like to hide another div on the page so is there a way to apply CSS to another div on the page if the data-booking-detail includes certain numbers?

So if data-booking-detail includes 7568 then hide div that offers book now and pay later.

答案1

得分: 0

首先,选择特定元素。在您的情况下,该元素具有ID tourmaster-payment-template-wrapper,您要查找的属性是包含值 7568data-booking-detail。这是如何隐藏第二个元素的示例:

#tourmaster-payment-template-wrapper[data-booking-detail*="7568"] + .hide-me {
  display: none;
}
<div class="tourmaster-template-wrapper" id="tourmaster-payment-template-wrapper" data-ajax-url="https://www.domainname.co.uk/wp/wp-admin/admin-ajax.php" data-booking-detail="{&quot;tour-id&quot;:&quot;7568&quot;}"></div>

<div class="hide-me">我应该被隐藏</div>
英文:

First, select the specific element. In your case, that element has the ID tourmaster-payment-template-wrapper and you are looking for the attribute data-booking-detail that contains the value 7568.

This is an example of how you could hide the second element:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#tourmaster-payment-template-wrapper[data-booking-detail*=&quot;7568&quot;] + .hide-me {
  display: none;
}

<!-- language: lang-html -->

&lt;div class=&quot;tourmaster-template-wrapper&quot; id=&quot;tourmaster-payment-template-wrapper&quot; data-ajax-url=&quot;https://www.domainname.co.uk/wp/wp-admin/admin-ajax.php&quot; data-booking-detail=&quot;{&amp;quot;tour-id&amp;quot;:&amp;quot;7568&amp;quot;}&quot;&gt;&lt;/div&gt;

&lt;div class=&quot;hide-me&quot;&gt;I should be hidden&lt;/div&gt;

<!-- end snippet -->

答案2

得分: -1

你可以使用CSS来根据data-booking-detail属性中的特定数字的存在来隐藏一个div。然而,仅凭CSS无法直接操作属性或其值。您需要使用JavaScript或jQuery来实现此功能。

以下是如何使用JavaScript来实现这一功能的示例:

<div id="book-now-div">优惠:现在预订,以后付款</div>

<script>
  // 获取data-booking-detail属性的值
  var bookingDetail = document.getElementById("tourmaster-payment-template-wrapper").getAttribute("data-booking-detail");

  // 检查bookingDetail是否包含特定的旅行ID
  if (bookingDetail.includes('7568')) {
    // 隐藏id为“book-now-div”的div
    document.getElementById("book-now-div").style.display = "none";
  }
</script>

这段代码使用JavaScript来检查data-booking-detail属性中是否包含数字"7568",如果包含,则隐藏id为"book-now-div"的div。

英文:

You can use CSS to hide a div based on the presence of a specific number in the data-booking-detail attribute. However, CSS alone cannot directly manipulate attributes or their values. You'll need to use JavaScript or jQuery to achieve this functionality.

Here's an example of how you can achieve this using JavaScript:

&lt;div id=&quot;book-now-div&quot;&gt;Offer: Book Now and Pay Later&lt;/div&gt;


&lt;script&gt;
  // Get the data-booking-detail attribute value
  var bookingDetail = document.getElementById(&quot;tourmaster-payment-template-wrapper&quot;).getAttribute(&quot;data-booking-detail&quot;);

  // Check if the bookingDetail includes the specific tour ID
  if (bookingDetail.includes(&#39;7568&#39;)) {
    // Hide the div with id &quot;book-now-div&quot;
    document.getElementById(&quot;book-now-div&quot;).style.display = &quot;none&quot;;
  }
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年6月8日 21:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定