javascript - 需要额外的功能标签吗?

标签 javascript jquery

this 示例有人展示了 jQuery onclick 事件。 我的显示是这样的:

$(function() {
    $('.post').on('click', function() {
        SOME STUFF
    });
});

但是第一行和最后一行在做什么? 如果我删除这些行,它就不起作用:

$('.post').on('click', function() {
    SOME STUFF
});

但是为什么呢?在链接的示例中是第二个评论者。他以这种方式展示(没有第一行/最后一行)。

有人能解释一下吗?

最佳答案

它是 $( document ).ready(...) 的快捷方式

参见http://api.jquery.com/ready/

引用文档:

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. [...]

All three of the following syntaxes are equivalent:
* $( document ).ready( handler )
* $().ready( handler ) (this is not recommended)
* $( handler )

关于javascript - 需要额外的功能标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23360197/

相关文章:

javascript - 单击每个表行中的复选框启用文本框

javascript - Express 中的 router.METHOD() 和 router.route() 方法有什么区别

javascript - LoDash 可以搜索动态对象数组,但不能搜索类型化对象数组

javascript - 动态 anchor 上的 jquery .click() 事件不起作用

javascript - Controller 从 Ajax 接收空数据

jQuery 在悬停时将 div 显示在其他 div 之上

javascript - 如何让 clickcount 函数从 0 以外的数字开始

javascript - 为什么 javascript 中的 indexOf 不起作用?

javascript - 按类别选择所有复选框,类别递增

javascript - 清除 Typeahead.JS 查询时缺少源错误