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