我有一些 HTML DOM 元素需要捕获点击事件,例如下拉菜单小部件等。
我想我们可以通过两种方式实现:
<a href="javascript:void(0);" id="option1">My text</a>
或者另一种方式是:
<span id="option2">My text 2</span>
在第二个选项中,我当然可以添加CSS属性cursor:pointer
。
我所需要的只是捕获单击事件,为此我可以为这两种情况使用相同的单击处理程序函数。
您认为我们有什么理由使用锚标记方法吗?
第二个选项看起来更干净。此外,IE 对于锚标记单击事件有默认行为,这可能会产生不良后果等。
I have some HTML DOM elements for which I need to capture click events, like a drop-down menu widget, etc.
I guess we can implement in 2 ways:
<a href="javascript:void(0);" id="option1">My text</a>
or another way is:
<span id="option2">My text 2</span>
In the second option, of course I can add CSS property cursor:pointer
.
All I need is to capture the click event, for which I can have the same click handler function for both the cases.
Do you think there is any reason why we would use anchor tag method?
The second option looks more clean. Also, the IE has a default behavior for anchor tag click events, which can have unwanted consequences, etc.
锚标记的一些好处:
Some benefits of the anchor tag: