触发jquery函数

时间:2011-06-15 03:00:24

标签: javascript jquery triggers

如何为特定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;
            }           
        });
    }
});

我的HTML看起来像这样:

<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>

我的解决方案是:

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

感谢您的帮助:)

1 个答案:

答案 0 :(得分:2)

$('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