[英]Disable W3.CSS Hover Dropdown
I have the typical W3.CSS hover dropdown, but I want it disabled until ready:我有典型的 W3.CSS 悬停下拉菜单,但我希望在准备好之前禁用它:
<div id="A" class="w3-dropdown-hover" style="width: 100%;">
<button id="B" disabled class="w3-button" style="width: 100%;">Hover To Open</button>
<div id="C" class="w3-dropdown-content w3-bar-block w3-border" style="width: 100%;">
<p>2B</p>
<p>|| !2B</p>
</div>
</div>
As you can see I have put the disabled
on the button
and it looks disabled as you would expect.如您所见,我已将
disabled
button
放在button
,它看起来像您期望的那样禁用。
But the dropdown still appears when I hover.
但是当我悬停时下拉菜单仍然出现。 I do not want that.
我不要那个。 I only want the dropdown to open when the button is not disabled.
我只希望在按钮未禁用时打开下拉菜单。
You obviously cannot put the disabled
on C
, because the disabled
only works on things like button
s, input
s, etc.您显然不能将
disabled
放在C
,因为disabled
只适用于button
、 input
等。
Where do I put the
disabled
?我把
disabled
放在哪里?
When I am ready in my javascript, how do I remove/reapply the
disabled
?当我准备好使用 javascript 时,如何删除/重新应用
disabled
? By that I mean, on which of "A", "B" or "C" do I apply the jQuery
$("#A_Letter").prop('disabled', false);
我的意思是,我在“A”、“B”或“C”中的哪一个上应用了 jQuery
$("#A_Letter").prop('disabled', false);
Edit: the only thing I can think of is put disabled
on the button and momentarily change the class of "A" to w3-dropdown-click
.编辑:我唯一能想到的就是在按钮上
disabled
并暂时将“A”的类更改为w3-dropdown-click
。
I'd apply some css to 'disable' thepointer events so your browser won't detect any hover actions:我会应用一些 css 来“禁用”指针事件,这样您的浏览器就不会检测到任何悬停操作:
<div id="A" class="w3-dropdown-hover" style="width: 100%; pointer-events: none;">
Demo Snippet:演示片段:
<link href="https://cdnjs.cloudflare.com/ajax/libs/w3-css/4.1.0/w3.css" rel="stylesheet"/> <div id="A" class="w3-dropdown-hover" style="width: 100%; pointer-events: none;"> <button id="B" disabled class="w3-button" style="width: 100%;">Hover To Open</button> <div id="C" class="w3-dropdown-content w3-bar-block w3-border" style="width: 100%;"> <p>2B</p> <p>|| !2B</p> </div> </div>
When I am ready in my javascript, how do I remove/reapply the disabled
当我准备好使用 javascript 时,如何删除/重新应用禁用的
For the automatically toggling both states, we can:对于自动切换两种状态,我们可以:
pointer-events:none
to a CSS class so we can use toggleClass()
pointer-events:none
移动到 CSS 类,以便我们可以使用toggleClass()
prop
to toggle the disabled stateprop
切换禁用状态 function toggle() { $("#A").toggleClass('disabled-hover'); $('#B').prop('disabled', (i, v) => !v); }
.disabled-hover { pointer-events: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/w3-css/4.1.0/w3.css" rel="stylesheet"/> <div id="A" class="w3-dropdown-hover disabled-hover" style="width: 100%;"> <button id="B" disabled class="w3-button" style="width: 100%;">Hover To Open</button> <div id="C" class="w3-dropdown-content w3-bar-block w3-border" style="width: 100%;"> <p>2B</p> <p>|| !2B</p> </div> </div> <button onclick='toggle()'>Toggle Disabled</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.