what's the difference between and

后端 未结 3 1364
野性不改
野性不改 2020-12-03 15:35

what is the difference between

and

One uses

相关标签:
3条回答
  • 2020-12-03 15:45

    One uses the parenthesis and not the other, but what are the differences of using either?

    <a onclick="someFunction"> won't do anything. The parenthesis cause a function to be called.

    Having a statement consisting of nothing but an identifier (be it a function name, variable, or whatever) won't do anything (except throw a reference error if the variable doesn't exist).

    And what happens if i dont use any href attribute?

    Then I'd question why you were using an <a> element in the first place.

    And also, is there a difference on assigning a inline onclick function to an anchor (a) that to other elements (e.g. span div label etc)?

    Only that they aren't (by default) focusable elements (nor is an a element without an href attribute), so the click event couldn't be triggered by tabbing to the element and pressing enter. If you want an element that will do something with JS when triggered, and you don't have a sensible fallback for when JS isn't available, use a button.

    0 讨论(0)
  • 2020-12-03 15:47

    when writing inline on click functions, we assigning the code to be executed in the form of string on click of the element.

    It is equivalent to eval('someFunction()'); we cannot write on click='someFunction' since it will be equivalent to eval('someFunction') which would do nothing. if you intend to bind a click handler to an anchor tag, dont forget to add a href='#' attribute to the anchor tag.

    There is no difference between assigning a click handler to span or divs as compared to anchor tag.

    0 讨论(0)
  • 2020-12-03 15:52

    The value of an event handler attribute is a sequence of Javascript statements, not an expression.

    It isn't assigning a function value to the property; it's a piece of code to execute at that event.
    Leaving out the parentheses, results in an expression statement that has no effect.

    0 讨论(0)
提交回复
热议问题