检查我看到的问题的HTML源代码:
<a id="comments-link-xxxxx" class="comments-link">add comment</a><noscript> JavaScript is needed to access comments.</noscript>
Run Code Online (Sandbox Code Playgroud)
然后在javascript源代码中:
// Setup our click events..
$().ready(function() {
$("a[id^='comments-link-']").click(function() { comments.show($(this).attr("id").substr("comments-link-".length)); });
});
Run Code Online (Sandbox Code Playgroud)
似乎所有用户点击事件都以这种方式绑定.
对于浏览网站没有javascript的人来说,这种方法的缺点是显而易见的,但是,javascript动态添加事件而不是直接声明它们有什么好处?
Dav*_*lan 18
最后一点,想象一下,如果您想在模板中的其他位置添加"显示评论"图标.将相同的回调绑定到图标非常容易.