“点击”之间的区别是什么?和“onclick”用jQuery创建元素时? [英] What's the difference between "click" and "onclick" when creating an element with jQuery?

查看:84
本文介绍了“点击”之间的区别是什么?和“onclick”用jQuery创建元素时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别

$("<a>", {
    "id" : "myId",
    "text" : "my link",
    "href" : "#",
    "onclick" : function() { return false; }
);

$("<a>", {
    "id" : "myId",
    "text" : "my link",
    "href" : "#",
    "click" : function() { return false; }
);

推荐答案

使用 onclick 创建属性,其值应该是引用函数的字符串,而不是实际函数。使用单击在元素上创建属性,其值应该是函数本身。

Using onclick creates an attribute, and its value should be a string that refers to a function, not an actual function. Using click creates a property on the element, and its value should be the function itself.

所以,第一个写得不正确;应该是这样的:

So, the first one is written incorrectly; should be like this:

$("<a>", {
    "id" : "myId",
    "text" : "my link",
    "href" : "#",
    "onclick" : "somefunction()"
} );

其中somefunction在全球范围内定义:

where "somefunction" is defined in the global scope:

window.somefunction = function() { return false; }

这篇关于“点击”之间的区别是什么?和“onclick”用jQuery创建元素时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆