简体   繁体   English

jQuery-双击类显示下拉列表以选择

[英]jQuery - double click class reveals drop down to select

I am having major trouble with this. 我对此有很大的麻烦。

I am trying to write something in jQuery, when text is double clicked it hides that element and shows a select drop down... However, when the user leaves that element, the drop down should go back to the original text. 我正在尝试在jQuery中写一些东西,当双击文本时,它会隐藏该元素并显示选择下拉列表。但是,当用户离开该元素时,下拉列表应该回到原始文本。

I have tried so many methods, and none work. 我尝试了很多方法,但没有任何效果。 I tried using dblclick with hover, etc. 我尝试将dblclick与悬停一起使用,等等。

In a short recap, user clicks div with text. 简要回顾一下,用户单击带有文本的div。 Div is hidden select is shown. 显示Div隐藏选择。 Users mouse leaves area, drop down is hidden, div is shown again. 用户鼠标离开区域,下拉菜单隐藏,再次显示div。

Thanks for the help, I have been playing with this for far too many hours! 感谢您的帮助,我已经玩了太多小时了!

Something like this doesn't work for you? 这样的东西对您不起作用?

HTML: HTML:

​<div>
    <span>TEXT TEXT TEXT</span>
    <select>
        <option>1</option>
        <option>2</option>
    </select>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

CSS: CSS:

span {
    cursor: default;
}

select {
    display: none;
}​

JS: JS:

$('div').dblclick(function() {
    $('span').hide();
    $('select').show();
}).mouseleave(function() {
    $('span').show();
    $('select').hide();
});​

Alternative JS: 替代JS:

$('div').dblclick(function() {
    $('span').hide();
    $('select').show();
});
$('select').mouseleave(function() {
    $('span').show();
    $('select').hide();
});​

The first JS will make the select box disappear when you move outside the bounds of the div (which are set by the size of the span text that was hidden). 当您移到div的边界外(由隐藏的跨度文本的大小设置)时,第一个JS将使选择框消失。 The second will will make the select box disappear once you've moused inside and then mouse out. 一旦将鼠标移入内部然后移出,第二个选择将使选择框消失。 If you'd prefer, you could change it from .mouseleave() to .change() and then it will only hide the select box when someone actually selects something. 如果愿意,可以将其从.mouseleave()更改为.change(),然后仅当有人实际选择内容时,它才会隐藏选择框。

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM