2017-08-08 105 views
-4

我有一个按钮,我已经给了类CSS:有没有办法停止通过键盘点击按钮?

.disabled{ 
    pointer-events: none ; 
    opacity: 0.6 ; 
} 

这意味着它不会完全禁止。所以我仍然可以通过javascript动态地点击它。 我的问题是,如果我使用我的键盘,然后单击选项卡,我能够达到按钮,并在点击输入它被点击。

有没有办法通过css禁用键盘事件,或使onfocus事件没有通过?所以我可以将该类添加到按钮并根据需要删除,谢谢。

+4

html按钮已禁用属性。你为什么不使用它? –

+1

尝试在该按钮元素上设置“tabindex =” - 1“'。 – debute

+1

你的意思是你不使用'disabled'属性的原因是你需要通过JS触发单元上的单击事件,但是'disabled'阻止了它的工作?那么在触发点击之前删除'disabled' _via JS_ ... – CBroe

回答

0

你的意思是

$(".disabled").prop("disabled",true); 
 

 
// and/or 
 

 
$(".disabled").on("click",function(e) { 
 
    console.log("clicked"); 
 
    e.preventDefault() 
 
});
<input type="text" placeholder="Tab to the button" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<button class="disabled">Click</button>

+0

不想使用禁用的财产! – user2549980

0

你可以叫你的按钮像这样

The disabled attribute is a boolean attribute. When present, it specifies that the element should be disabled.A disabled element is unusable.The disabled attribute can be set to keep a user from using the element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the element usable.

<button type="button" class="disabled" disabled>click</button> 

尝试在这个片段中

.disabled{ 
 
    pointer-events: none ; 
 
    opacity: 0.6 ; 
 
}
<button type="button" class="disabled" disabled>click</button>

2

在这里,你去了一个解决方案https://jsfiddle.net/pj3heuso/

$('button').click(function(){ 
 
    console.log($(this).text()); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button type="submit"> 
 
Submit 1 
 
</button> 
 

 
<button type="submit" tabindex="-1"> 
 
Submit 2 
 
</button>

button使用键盘将永远不会获得焦点,因为tabindex="-1"

相关问题