开发者

Is better: <a href="/test" onclick="T.actions.addFriend(profileId)">add friend</a> or ...?

Is better: <a href="/test" onclick="T.actions.addFriend(profileId)">add friend</a>

or

<a href="/test" class="add-friend-button">add friend</a>

and then with the Javascript select the 开发者_Python百科"add-friend-button" class and add a click event?


Depends on personal taste. Adding the onclick later on is consired to be "better", "modular" etc. However having your HTML document filled with <script> tags might be even uglier.

Also read the Wikipedia article on Unobstrusive Javascript (UJS).


"and then with the Javascript select the "add-friend-button" class and add a click event?"

Yes, this one is better. You want your markup to be clean (non intrusive javascript).


If this button is to be used on multiple pages, certainly register the event with the class. You could then place the registration in an external js file and include it with each page that uses this type of button.

The concept is analogous to CSS. You only want to define your style code or js code in one place, whether that is inline, per page, or an included file depends on what you are trying to accomplish.


Adding the "onclick" later is also slower! (I know just a little bit slower so it won't decrease the vieweing experince of your webpage)

If you need to add it only in one place on your page you could also put the onclick directly inline, but if you are adding these onclick events in many parts of your page I would recommend separating and being unobtrusive (by adding onclick later in a script or in window.onload event).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜