简体   繁体   中英

Triggering a jquery function

How can I trigger the following function for a specific DIV

$(document).ready(function() {
    if($('div.trigger').length > 0) {
        $('div.trigger').click(function() {
            if ($(this).hasClass('open')) {
                $(this).removeClass('open');
                $(this).addClass('close');
                $(this).next().slideDown(100);
                return false;
            } else {
                $(this).removeClass('close');
                $(this).addClass('open');
                $(this).next().slideUp(100);
                return false;
            }           
        });
    }
});

My html looks like this:

<div class="trigger open"><a href="#"><strong>Dropdown 1</strong></a></div>
<div class="cnt">
    <a href="?library&delasset=1">foo</a><br>
    <a href="?library&delasset=2">bar</a>
</div>

<div class="trigger open"><a href="#"><strong>Dropdown 2</strong></a></div>
<div class="cnt">
    <a href="?library&delasset=3">baz</a><br>
    <a href="?library&delasset=4">boo</a>
</div>

My solution was to this:

$(document).ready(function()
{
    $('div.trigger:eq(<?=$_GET[exp];?>)').trigger('click');
});

Thanks for the help:)

$('div.trigger:eq(0)').trigger('click');//trigger click event of the Oth element
$('div.trigger:first').trigger('click');//trigger click event of first
$('div.trigger:last').trigger('click');//trigger the click of last one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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