英文:
WordPress site - applying CSS when certain criteria are met
问题
这是网站上用于特定预订的
<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="{"tour-id":"7568"}">
对于特定的旅行ID,我想要隐藏页面上的另一个
所以如果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.
<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;} >
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
,您要查找的属性是包含值 7568
的 data-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="{"tour-id":"7568"}"></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*="7568"] + .hide-me {
display: none;
}
<!-- language: lang-html -->
<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">I should be hidden</div>
<!-- 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:
<div id="book-now-div">Offer: Book Now and Pay Later</div>
<script>
// Get the data-booking-detail attribute value
var bookingDetail = document.getElementById("tourmaster-payment-template-wrapper").getAttribute("data-booking-detail");
// Check if the bookingDetail includes the specific tour ID
if (bookingDetail.includes('7568')) {
// Hide the div with id "book-now-div"
document.getElementById("book-now-div").style.display = "none";
}
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
- ajax
- css
- jquery
评论