簡體   English   中英

如果p元素在jQuery之前,JQuery不會隱藏div?

[英]JQuery does not hide div if p element is before it?

我正在嘗試利用JQuery代碼段在SharePoint上隱藏一些內容。 它沒有<p>元素就可以工作,但是SharePoint在div之間有很多東西,所以我認為這就是為什么它不起作用的原因。

JQuery的:

jQuery(document).ready(function() {
jQuery(".ms-wpcontentdivspace").hide();
//toggle the componenet with class msg_body
jQuery(".ms-webpart-chrome-title").click(function(e)
 {
   e.preventDefault();
   jQuery(this).next("div.ms-wpcontentdivspace").slideToggle(200);
 });
});

這是工作代碼:

<div class="ms-webpart-chrome-title"><h2><a href="http://google.com"><span>Hello</span>      </a></h2></div>
<div class="ms-wpcontentdivspace">Hi there. I am hidden</div>
<div class="ms-webpart-chrome-title"><h2>Another title</h2></div>
<div class="ms-wpcontentdivspace">More hidden stuff</div>

這是沒有的代碼:

<div class="ms-webpart-chrome-title"><h2><a href="http://google.com"><span>Hello</span></a></h2></div>
<p>some text</p>
<div class="ms-wpcontentdivspace">Hi there. I am hidden</div>
<div class="ms-webpart-chrome-title"><h2>Another title</h2></div>
<div class="ms-wpcontentdivspace">More hidden stuff</div>

這是一個Jsfiddle 謝謝!

使用1.3.x的jQuery版本已經很舊了,還是嘗試

jQuery(document).ready(function () {
    jQuery(".ms-wpcontentdivspace").hide();
    //toggle the componenet with class msg_body
    jQuery(".ms-webpart-chrome-title").click(function (e) {
        e.preventDefault();
        jQuery(this).nextAll("div.ms-wpcontentdivspace").eq(0).slideToggle(200);
    });
});

演示: 小提琴

$.next()僅獲得緊隨其后的兄弟,因此您將需要使用$.nextAll()

jQuery(document).ready(function() {
jQuery(".ms-wpcontentdivspace").hide();
//toggle the componenet with class msg_body
jQuery(".ms-webpart-chrome-title").click(function(e)
 {
   e.preventDefault(); 
   jQuery(this).nextAll("div.ms-wpcontentdivspace").eq(0).slideToggle(200);
 });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM