2016-02-29 57 views
0

我試圖讓月份顯示爲通過閱讀與編號爲博客日期閱讀的div發佈和刪除顯示無類在每月左右的div上,即Feb2016CC如果div id包含文本,從另一個div(jQuery)中刪除類

博客的日期顯示是這樣的:

<div class="blog-date blog-date-2" id="blog-date-read">Feb 2016</div> 

博客的幾個月是這樣的:

<div id="FebCC2016" class="display-none-blog"> 
<p><a href="#">Feb 2016</a></p> 
</div> 

而jQuery的我試圖像這個樣子:

if ($("div#blog-date-read:contains('Feb 2016')")) { 
$("#FebCC2016").removeClass("display-none-blog"); 
} 

我我確定我錯過了我的jQuery,但我不知道是什麼。有人可以用這個指向正確的方向嗎?

編輯:

如果任何人遇到這樣的未來,我結束了使用此解決方案:

if ($(".blog-date-2").text().indexOf("Feb 2016") >= 0) { 
    $("#FebCC2016").removeClass("display-none-blog"); 
} 

回答

0

你在你的ID選擇失蹤#blog-date-read-read

if ($("div#blog-date-read:contains('Feb 2016')")) { 
    $("#FebCC2016").removeClass("display-none-blog"); 
} 
+0

我已經更新了這個,但不幸的是它仍然不適合我。 ): – Trees

+0

必須注意':contains'中的文本是區分大小寫的,還有一件事,你是否有多個元素使用這個'id =「blog-date-read」'id name? –

+0

我意識到每次出現的日期都使用相同的ID,所以是的。我試圖用這個:div.blog-date-2:contains('Feb 2016')搜索每個div與沒有運氣附加的類。 – Trees

0

alert($("div.blog-date").text()) 
 
if ($("div.blog-date").text().toLowerCase() == 'feb 2016') { 
 
$("#FebCC2016").removeClass("display-none-blog"); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> 
 
<div class="blog-date blog-date-2" id="blog-date-read">Feb 2016</div> 
 

 

 
<div id="FebCC2016" class="display-none-blog"> 
 
<p><a href="#">Feb 2016</a></p> 
 
</div>
從您的

首先改變條件.,因爲它是一個類,然後使用text()獲取內容嘗試這種方式。

+0

我得到了警報,但它並沒有消除這個類。 – Trees

+0

檢查開發工具班級不見了@Trees – guradio

+0

不幸的是,它不能在頁面上工作。再次,它不刪除類,只返回警報。這意味着什麼? – Trees

相關問題