在页面加载时运行jquery函数

时间:2015-10-08 09:18:08

标签: jquery

请有人帮助修改以下内容,以便在页面加载时触发,而不是在单击按钮时触发。

$(function () {
    $('a[href="#search"]').on('click', function(event) {
        $('#search').addClass('open');
        $('#search > form > input[type="search"]').focus();
    });

    $('#search, #search button.close').on('click keyup', function(event) {
        if (event.target == this || event.target.className == 'close' || event.keyCode == 27) {
            $(this).removeClass('open');
        }
    });
});

1 个答案:

答案 0 :(得分:0)

只需从click事件中获取代码:

<script type="text/javascript">
$(function () {
    $('#search').addClass('open');
    $('#search > form > input[type="search"]').focus();

    $('#search, #search button.close').on('click keyup', function(event) {
        if (event.target == this || event.target.className == 'close' || event.keyCode == 27) {
            $(this).removeClass('open');
        }
    });
});
</script>