最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's the difference between "click" and "onclick" when creating an el

programmeradmin7浏览0评论

What's the difference between

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

and

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

?

What's the difference between

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

and

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

?

Share Improve this question asked Dec 13, 2012 at 4:20 supertonskysupertonsky 2,7438 gold badges39 silver badges78 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 17

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()"
} );

where "somefunction" is defined in the global scope:

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论