[英]Adding New Element during `mousedown` event prevent triggering `click` event?
[英]Click (mousedown) event of a select element
我有一个空的select元素,我想在单击该元素时执行一些代码,但是我不希望出现下拉列表,这是我发现的内容:
为了争辩,我想要的是能够单击select元素,JavaScript引发一个警报框,但是我想阻止显示下拉列表。
谢谢!
捕获select元素的onmousedown
事件,并通过将event.returnValue
设置为false来取消该事件。
<script type="text/javascript">
function onSelectMouseDown() {
event.returnValue = false;
alert("mouse down caught and cancelled");
}
</script>
<select id="select" onmousedown="onSelectMouseDown();"></select>
为什么不禁用select元素,然后将select元素包装在与select元素高度和宽度相同的div(或其他宽度)中,并在此div中使用mousedown事件?
<script>
function makeDisable(){
var x=document.getElementById("mySelect")
x.disabled=true
}
function makeEnable(){
var x=document.getElementById("mySelect")
x.disabled=false
}
function f() {
makeDisable();
alert("something");
t=setTimeout("makeEnable();",1);
}
</script>
<select id="mySelect" onMouseDown="f();">
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.