简体   繁体   English

jQuery:将类追加到上一个元素并使其可单击

[英]jQuery: Append class to previous element and make it clickable

I'm just starting out with jQuery and I'm working on a mobile web site which has multiple drop down elements (forms, lists, carousels etc.) on the same page. 我刚开始使用jQuery,正在一个移动网站上工作,该网站在同一页面上具有多个下拉元素(表单,列表,轮播等)。 For getting multiple drop downs I used Andy Langton's code snippet . 为了获得多个下拉菜单,我使用了Andy Langton的代码段

The dropdowns work just fine but they append the drop down links (plus and minus signs) at the end of the previous element. 下拉菜单工作得很好,但是它们将下拉链接(加号和减号)附加在上一个元素的末尾。 What I'd like to do is make the entire element itself clickable (in this case a header). 我想做的是使整个元素本身都可单击(在本例中为标题)。 Is there any way to do so using this snippet? 有使用此代码段的方法吗? The code can be seen here . 该代码可以在这里看到。

Any advice would be greatly appreciated. 任何建议将不胜感激。 Thanks! 谢谢!

This works!: http://jsfiddle.net/yQme5/27/ 这有效!: http : //jsfiddle.net/yQme5/27/

You (almost) only have to change the click event and put it on $('.hdropdown') instead of $('a.toggleLink') and when you change the link depending on whether the element is shown or hidden to change, modify: 您(几乎)只需更改click事件并将其放到$('.hdropdown')而不是$('a.toggleLink')并且当您根据元素是显示还是隐藏来更改链接时,修改:

$(this).html( (!is_visible) ? showText : hideText); ; ;

to

$(this).find('a').html( (!is_visible) ? showText : hideText);

the following works perfect 以下作品完美

<a href="javascript://"class="toggleLink" >
    <h3 class="hdropdown">Clickable Header</h3>
</a>
<div class="toggle">
        This is some random text. This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.
</div>

<a href="javascript://"class="toggleLink" >
    <h3 class="hdropdown">Clickable Header 2</h3>
</a>
<div class="toggle">
This is some random text. This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.This is some random text.
</div>

css style CSS风格

.hdropdown {display:block; font-family:Verdana, sans serif; background:#DEDEDE;border:1px solid #CCCCCC;}

.toggle{
    display:none;
}

jquery code jQuery代码

$(document).ready(function() {

    // capture clicks on the toggle links
    $('a.toggleLink').click(function() {

        $(this).next('div.toggle').slideToggle('slow');

        // return false so any link destination is not followed
        return false;

    });
});

you can try the live demo at js fiddle 您可以在js小提琴上尝试现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM