javascript - 单击主选择框时如何自动打开第二个选择框?
问题描述
我有两个重叠(100% 重叠)的选择下拉菜单,我希望能够单击一个与可打开的选择下拉菜单重叠的下拉菜单,同时触发另一个选择下拉菜单以打开选项列表。我已经看到引用这是不受支持的,如果是这样,我需要一种解决方法来获得选项以保留覆盖选择的值,同时仅打开第一个选择框 z-index 小于第一个选择框。
我该如何完成这个要求?
小提琴
https://jsfiddle.net/0rgthvub/
HTML
<select class="long-select" style="background-color:white; position: absolute !important; width: 200px; z-index: 10 !important;" onmousedown="(function(event){processOpen(event);})(event, this)">
<option value="Open - some description" selected></option>
</select>
JavaScript/jQuery
function processOpen(e) {
e.preventDefault();
$input = $("select.shortened-select");
var $this = $(this);
if ($input.is("select") && !$('.lfClon').length) {
var $clon = $input.clone();
var getRules = function(e){ return {
position: 'absolute',
left: e.offset().left,
top: e.offset().top,
width: e.outerWidth(),
height: e.outerHeight(),
opacity: 0,
margin: 0,
padding: 0
};};
var rules = getRules($this);
$clon.css(rules);
$clon.on("mousedown.lf", function(){
debugger;
$clon.css({
marginLeft: $input.offset().left - rules.left,
marginTop: $input.offset().top - rules.top,
});
$clon.on('change blur', function(){
$input.val($clon.val()).show();
$clon.remove();
});
$clon.off('.lf');
});
$clon.on("mouseout.lf", function(){
$(this).remove();
});
$clon.prop({id:'',className:'lfClon'});
$clon.appendTo('body');
}
value = document.querySelectorAll(".shortened-select option")[document.querySelector(".shortened-select").selectedIndex].value;
$input.value = "value";
$input.value.show();
}
function focus() {
[].forEach.call(this.options, function(o) {
if (o.val === ""){
o.setAttribute('data-descr', document.querySelectorAll(".shortened-select option")[document.querySelector(".shortened-select").selectedIndex].getAttribute("val"));
o.selected = 'selected';
}
else
{
o.textContent = o.getAttribute('val');
}
});
}
function blur(e) {
[].forEach.call(this.options, function(o) {
o.textContent = o.getAttribute('val') + ' - ' + o.getAttribute('data-descr');
});
this.blur();
}
function mouseup(e) {
[].forEach.call(this.options, function(o) {
o.textContent = o.getAttribute('val') + ' - ' + o.getAttribute('data-descr');
});
}
[].forEach.call(document.querySelectorAll('.shortened-select'), function(s) {
s.addEventListener('focus', focus);
s.addEventListener('change', blur);
blur.call(s);
});
解决方案
推荐阅读
- google-sheets - 通过 ODBC 驱动程序连接 Power BI 的 BigQuery
- javascript - 如何在 javascript 中获取两个 api?
- c# - 从招摇响应中排除 ef 虚拟属性
- vb.net - 是否可以执行`Await Task.Run(Sub() SomeDatabaseUpdate() EndSub)
- java - 打印对象的arrayList
- c - 为什么 MAP_GROWSDOWN 映射不增长?
- ios - 如何在 ARKit 中将 3D 对象放置在垂直平面中?
- docker - 在不同的节点上运行两个 Kubernetes pod
- powershell - 如何从 Outlook 中阅读最新的未读电子邮件,直到我已阅读邮件
- kubernetes - 你如何运行`kubectl -f apply
` 来自使用 Cloud Composer 中的 bash 运算符的 DAG?