使用“这个”与jQuery选择器 [英] Using "this" with jQuery Selectors

查看:94
本文介绍了使用“这个”与jQuery选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些HTML如下:

I have some HTML that looks like this:

<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;我想使用jQuery显示或隐藏'p'标记时,单击锚点,但我有一些麻烦与兄弟选择器。

Using CSS I'm setting the 'p' tag to display:none;. I want to use jQuery to display or hide the 'p' tag when the anchor is clicked, but I'm having some troubles with the sibling selector.

只是想让选择器工作,我试过:

Just trying to get the selector working, I tried:

$("a.question").click(function () {
    $(this + " ~ p").css("background-color", "red");
});

来测试它。看起来,兄弟选择器不能像这样使用,因为我是全新的jQuery我不知道适当的方法,使这种情况发生。

to test it out. Seemingly, the sibling selector can't really be used like that, and as I'm completely new to jQuery I don't know the appropriate means to make that happen.

提前感谢!

推荐答案

>

Try using:

$(this).siblings('p').css()

这篇关于使用“这个”与jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆