JavaScript event attachment

As a commentary to one of the questions here, the commentator wrote (my accent):

... Using the built-in onclick, you do something similar, but it's harder to maintain and subject to problems. The JavaScript community as a whole has been moving away from embedded JavaScript for some time now.

This applies to attaching events to HTML elements using

$("#someID").click(function(){
    do something here...;
});

but not

<a id="someID" onclick="someFunction();">

Is there really a transition from the old school way of declaring events inline, and if so, what are the advantages of one of the others?

EDIT I think it might be helpful to include a link to the original question . He asked about attaching a different click event to each tab. Is my answer shit and I have to apologize to FallenRayne =).

+3
4

(html) / (javascript). javascript. , .

+4

, , , - , , .

, :

  • Cleaner/Less

:

<a onclick="someFunction();">
<a onclick="someFunction();">
<a onclick="someFunction();">
<a onclick="someFunction();">
<a onclick="someFunction();">

:

$("a").click(someFunction);

css .., . , , . , : $('a').unbind('click').click(...something new...);

. .js, , - . - = .

, , , jQuery, ?

$("li").hover(function() {
  $(this).children().slideToggle();
});

, , ( mouseenter/mouseleave, mouseover/mouseout... , .hover(), )

+4

inline

, , JS-, jQuery, Prototype .., " ".

, ?

HTML JavaScript (, , ). , , , , - , , JS .

+3

, , . , , javascript, , DRY.

0
source

Source: https://habr.com/ru/post/1740600/


All Articles