Does cursor:pointer
property not simulate behavior for a given situation? If so, why is it set via CSS and not just by JavaScript?
JS does not simulate the behavior of the element, it simulates the behavior of the mouse cursor. Notice that HTML has several elements that are conventionally used by the mouse cursor to give a visual feedback to the user. For example, the <a>
tag uses cursor:pointer
and the <input>
tag uses cursor:text
. Putting a cursor:text
into a <div>
will not change the behavior of <div>
will only confuse the user because he can understand that it is possible to type text in div
...
There are a number of cursor types. MicroSoft itself has addressed this in this article: link
Well-designeduserinterface(UI)objectsareconsidered suchasaffordance,whicharevisualandbehavioralproperties ofanobjectthatsuggestshowitisused.Thepointeractsasa proxyforthehand,allowinguserstointeractwith screenmuchlikephysicalobjects.
Thismeansthatthecursorisanextensionoftheuser'shand,andittakesintoaccountthetypeofthepointertoknowhowitwillinteractwiththeelement,whetheritisclicking,dragging,typing,etc...
Anotherpointisaboutaccessibility.The<button>
,<label>
,and<input>
tagsareinterpretedinawaybythescreenreaders,andthelittle"cursor type" will matter to them. The main one in this case is to worry about code semantics, the correct use of roles
and aria
attributes can read about them here: link
Note: Right-clicking, double-clicking, and clicking with the Shift
or Ctrl
key modifiers are three mouse interactions that are not intuitive , because they have no counterparts in the real world.
What does JavaScript actually do when adding this cursor? Add a style to the element with CSS?
Yes it adds the style to the time, but changing the cursor is not making a :hover
. Notice that your code changes the cursor by putting a cursor:pointer
style directly in the tag (type a style inline
), but when you take the cursor off the element it continues with cursor:pointer
set in the tag. What your script does is not exactly :hover
and a screen reader would not understand that :hover
was made and would not "see" that it was changed ...
The:hover
willalwaysexist,oncetheDOMandCSSOMisbuiltallelementswillalreadyhavetheirstyleof:hover
declaredbyCSS,alreadywithJSyouwillonlyapplythestylewhentheeventhappensintheelement,somethingthatmayevendependonthehardwareortheuser'sconnectiontohaveagoodperformance.Asyousawintheimagethecursorstyledoesnotyetexistuntiltheuserinteractswiththeelement.WithCSSalreadythestylewillalreadybe"programmed", it is something like " will change
", where the browser already knows that element will undergo an interaction or animation. link
Regarding other CSS properties added on an element, does it "take" the same attitude as the cursor?
I think so, but I can not say exactly how the browser and screen readers understand JS, because in most cases they are events that are only triggered when the user interacts. Some of the events do not run when the page loads, but only when it has some action on the part of the user. What I can say is that the cursor type does not change the type of the element.
Read: