[英]Using “this” with jQuery Selectors
我有一些看起來像這樣的HTML:
<ul class="faq">
<li class="open">
<a class="question" href="">This is my question?</a>
<p>Of course you can, it will be awesome. </p>
</li>
</ul>
使用CSS我將p
標簽設置為display:none;
。 單擊anchor
時,我想使用jQuery顯示或隱藏p
標簽,但是同級選擇器遇到了一些麻煩。
只是試圖使選擇器工作,我嘗試過:
$("a.question").click(function () {
$(this + " ~ p").css("background-color", "red");
});
測試一下。 看來,同級選擇器實際上不能像那樣使用,而且由於我是jQuery的新手,所以我不知道實現該目標的適當方法。
嘗試使用:
$(this).siblings('p').css()
$(this).next("p").css("...")
如果您只想要DOM中的下一個非空白節點,則上面的“ p”是可選的。
我想使用jQuery在單擊錨點時顯示或隱藏'p'標簽
既然您提到您想在單擊錨點時切換'p'標簽,所以我願意:
$("a.question").click(function (event) {
$(this).siblings('p').show(); //toggle the p tags that are siblings to the clicked element
event.preventDefault(); //stop the browser from following the link
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.