禁用按钮时按钮的标签文本仍然可单击
我有一个通过数据绑定禁用的按钮。 运行时该按钮呈灰色。 当用户将光标悬停在按钮的非文本区域上时,该按钮将充当禁用按钮 - 用户无法单击它。 但是,如果用户将光标悬停在按钮中的文本标签上,则可以单击它。 为什么?
<div class="flextable-item">
<div class="btn-toolbar dashhead-toolbar float-left">
<a href="@Url.Action("Edit", "VendorManagement", new { area = "Vendor" })/@Model.Id">
<button type="button" class="btn btn-primary-outline" data-bind="enable: EnableDetail">
<span class="icon icon-pencil" > Edit</span>
</button>
</a>
</div>
</div>
I have a button that I have disabled via a data-bind.
At runtime the button is grayed out.
When a user floats their cursor over the non-text area of the button, then the button acts as a disabled button - the user cannot click on it.
However, if the user floats their cursor over the text label in button, then they can click on it.
Why?
<div class="flextable-item">
<div class="btn-toolbar dashhead-toolbar float-left">
<a href="@Url.Action("Edit", "VendorManagement", new { area = "Vendor" })/@Model.Id">
<button type="button" class="btn btn-primary-outline" data-bind="enable: EnableDetail">
<span class="icon icon-pencil" > Edit</span>
</button>
</a>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当直接单击按钮时,
disabled
属性会阻止触发单击事件。当您单击按钮的任何子元素时,事件将冒泡到父元素。该按钮不会触发自己的单击处理程序,但会将事件传递给其自己的父级。
您可以通过附加返回
false
的点击处理程序来阻止元素的默认行为:
注意:下面的代码片段实际上不允许
打开一个新选项卡,但您可以检查浏览器控制台以查看其在
enabled() === true
时尝试执行的默认行为。When the button is clicked directly, the
disabled
attribute prevents a click event from being triggered.When you click any of the button's child elements, the event will bubble up to the parents. The button won't fire its own click handlers, but it will pass on the event to its own parent.
You can prevent the
<a>
element's default behavior by attaching a click handler that returnsfalse
:Note: the snippet below won't actually allow the
<a>
to open a new tab, but you can check the browser console to see its default behavior trying to execute whenenabled() === true
.