[英]Target a div ID that appears on a page containing a specific class - CSS or jQuery? Both?
我對此很新,所以如果我沒有正確解釋,或者沒有提供正確的代碼(或提供太多代碼),請原諒我。
所以在我的頁面頂部附近我有這個代碼:
<div id="javo-detail-item-header-wrap" class="container-fluid javo-spyscroll lava-spyscroll" style="padding: 0px; z-index: 1;">
然后在頁面下方:
<div id="post-2355" class="single-item-tab post-2355 property type-property status-publish has-post-thumbnail hentry property_type-ny property_status-sold">
我想從第一行代碼中定位#javo-detail-item-header-wrap
,但僅當.property_status-sold
也在頁面上時。
根據我的理解,它的后代無法通過CSS定位元素,對嗎? 我已經看到在網上提到通過用jQuery重命名第一個ID來解決類似情況 - 這是需要做什么的?
如果它不在同一個div中,那么第二個元素甚至是第一個元素的后代嗎?
嘗試這個 :
jQuery(document).ready(function(){
var myclass = jQuery(".property_status-sold");
if(myclass != undefined)
{
// Write whatever you want..
jQuery("#javo-detail-item-header-wrap").show();
}
});
如果沒有看到完整的代碼結構,我會說99%使用純CSS是不可能的。 在JavaScript中執行它應該很容易:
function getIt() {
if (document.getElementsByClassName('property_status-sold').length > 0)
return document.getElementById('javo-detail-item-header-wrap');
}
要獲取你的元素,只需調用函數getIt
(並給它一個更好的名字) - 它將返回元素引用,或者如果找不到.property_status-sold
或者實際元素#javo-detail-item-header-wrap
則返回undefined
由於某種原因,缺少#javo-detail-item-header-wrap
。
如果你真的非常喜歡jQuery,這里是上面的代碼(注意它返回一個jQuery對象,而不是元素引用 - 你可以使用.get(0)
到元素引用):
function getIt() {
if ($('.property_status-sold').length > 0)
return $('#javo-detail-item-header-wrap');
}
隱藏該元素的快速修復:
jQuery(document).ready(function(){
if (jQuery('.property_status-sold').length > 0)
jQuery('#javo-detail-item-header-wrap').hide();
})
這就是你所需要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.