- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个带有相邻下拉列表的输入字段,其想法是输入字段可以接受用户输入的文本或从下拉列表中选择的值。为了使事情尽可能简单,我使用样式在输入具有 focus
时排队,然后显示下拉列表。问题在于,当单击某个元素时,输入失去焦点并且下拉列表消失,从而无法检测到单击操作。
有没有简单的方法解决这个问题?理想情况下,我想让事情保持原样 我想知道是否有不同的方式可以听取点击以填充表单字段?代码如下:
<!doctype html>
<html>
<head>
<title>jQuery editable select</title>
<link rel="stylesheet" href="css/bootstrap.min.css" />
<style>
.wrap { width: 1024px; margin: 0 auto; }
.left { width: 600px; float: left; }
.right { width: 400px; float: right; }
.editable-select-wrap {}
.editable-input {}
.editable-input:focus + .editable-drop { display: block; }
.editable-drop { list-style: none; padding: 0; margin: 0; border: 1px solid #eee; display: none; }
.editable-drop.focus { display: block; }
.editable-drop li { display: block; padding: 10px; border-bottom: 1px solid #eee; cursor: pointer; }
.editable-drop li:last-child { border-bottom: none; }
</style>
</head>
<body>
<div class="wrap">
<div class="left">
<span class="editable-select" data-name="phone" data-options='[{"val" : "1","text" : "Option 1"}, {"val" : "2","text" : "Option 2"}]'></span>
</div>
<div class="right">
<span class="editable-select" data-name="message" data-options='[{"val" : "1","text" : "Option 1"}, {"val" : "2","text" : "Option 2"}]'></span>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
( function ( $ ) {
$.fn.editableSelect = function( options ) {
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Configuration Settings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
var config = $.extend({
bootstrap: false,
classDefault : 'editable-input',
classbootstrap: 'form-control'
}, options );
var html = classes = '', json;
if( config.bootstrap ) {
classes = config.classbootstrap +' '+ config.classDefault;
} else {
classes = config.classDefault;
}
$( this ).each( function() {
html = '';
html = '<div class="editable-select-wrap">';
html += '<input type="text" name="'+ $( this ).data('name') +'" class="'+ classes +'" />';
html += '<ul class="editable-drop">';
json = $( this ).data('options' );
$( json ).each( function() {
html += '<li data-value="'+ this.val +'">'+ this.text +'</li>';
});
html += '</ul></div>';
$( this ).html( html );
});
var parent;
$('body').on('click', '.editable-drop li', function() {
console.log( 'fire' );
parent = $( this ).parent();
parent.siblings('input').val( $( this ).text() );
parent.css('display', 'none');
});
};
}) ( jQuery );
$('.editable-select').editableSelect({ bootstrap: true });
</script>
</body>
</html>
如有任何建议,我们将不胜感激!!!
这是代码的 fiddle ,我删除了引导 Assets :
最佳答案
我通过向 editable-drop
添加 hover
找到了解决此问题的方法
.editable-drop:hover {
display:block;
}
工作 JSFiddle:http://jsfiddle.net/xL1kbfne/1/
或下面的代码片段:
( function ( $ ) {
$.fn.editableSelect = function( options ) {
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Configuration Settings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
var config = $.extend({
bootstrap: false,
classDefault : 'editable-input',
classbootstrap: 'form-control'
}, options );
var html = classes = '', json;
if( config.bootstrap ) {
classes = config.classbootstrap +' '+ config.classDefault;
} else {
classes = config.classDefault;
}
$( this ).each( function() {
html = '';
html = '<div class="editable-select-wrap">';
html += '<input type="text" name="'+ $( this ).data('name') +'" class="'+ classes +'" />';
html += '<ul class="editable-drop">';
json = $( this ).data('options' );
$( json ).each( function() {
html += '<li data-value="'+ this.val +'">'+ this.text +'</li>';
});
html += '</ul></div>';
$( this ).html( html );
});
var parent;
$('body').on('click', '.editable-drop li', function() {
console.log( 'fire' );
parent = $( this ).parent();
parent.siblings('input').val( $( this ).text() );
parent.css('display', 'none');
});
};
}) ( jQuery );
$('.editable-select').editableSelect({ bootstrap: true });
.wrap { width: 1024px; margin: 0 auto; }
.left { width: 600px; float: left; }
.right { width: 400px; float: right; }
.editable-select-wrap {}
.editable-input {}
.editable-input:focus + .editable-drop { display: block; }
.editable-drop { list-style: none; padding: 0; margin: 0; border: 1px solid #eee; display: none; }
.editable-drop.focus { display: block; }
.editable-drop li { display: block; padding: 10px; border-bottom: 1px solid #eee; cursor: pointer; }
.editable-drop li:last-child { border-bottom: none; }
.editable-drop:hover {
display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
<div class="left">
<span class="editable-select" data-name="phone" data-options='[{"val" : "1","text" : "Option 1"}, {"val" : "2","text" : "Option 2"}]'></span>
</div>
<div class="right">
<span class="editable-select" data-name="message" data-options='[{"val" : "1","text" : "Option 1"}, {"val" : "2","text" : "Option 2"}]'></span>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
关于一个元素的 jQuery onfocus 有另一个元素可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27048570/
有没有什么办法,如果您在同一页面上有 2 个或更多打开的选项卡,当您聚焦一个选项卡时,所有其他选项卡将立即收到“focus()”通知? 我以 Facebook Chat 为例。如果您收到新消息,所有选
现在我正在更改 onfocus(onclick 等) 事件的默认行为,如下所示: function my(e){ e.style.backgroundColor = "red";//her
一开始我有输入字段,它与 jquery 代码配合得很好,现在我将输入字段更改为 asp.net 文本框,因为我进行了扩展,因此它将数据插入数据库中。所以现在我需要弄清楚它如何与 asp:textbox
我在使 onFocus 和 onBlur 事件正常工作时遇到一些问题 这是我得到的 var var1 $("input").focus(function() { var1 = $(this).va
我有一个字段,我想在其中选择一个输入框来更改字段的背景颜色。我尝试通过一个简单的 onfocus 操作更改输入父级的 html 类,即字段,但我没有看到任何反应。 我有以下内容,并仔细检查了类名。 在
我有很多文本区域文件,我希望它们在聚焦时全部展开 这是我的代码演示:http://jsfiddle.net/PU73y/ 问题是当我点击它们时它们不会展开。这是为什么? 谢谢 this is test
我正在编写一个 JavaScript 聊天应用程序,但我遇到了一个小问题。 这是 HTML 结构: 当用户点击/聚焦聊天框时,我希望文本框自动聚焦。我在聊天框上有这个 onfo
我在谷歌上寻找一种方法来删除焦点文本,但无法弄清楚如果字段中没有输入任何内容,我们如何将其添加回去。 这就是我目前所拥有的... onfocus="if(this.value == 'Name*')
我使用 onfocus 事件处理程序来做几件事。它在初始页面加载后效果很好。但是,在我单击打开模式弹出窗口的链接后,onfocus 事件停止工作。焦点仍然可以正常工作...但是 onfocus 事件永
当我动态附加输入时,onfocus 函数不起作用。 我的代码 $i=1; )"> 我的功能是 function myFun(val){ alert(val); } 最佳答
在使用 Internet explorer 的页面上,我有一个下拉菜单,但在第一次加载页面时,因为它上面有一个 onFocus,所以需要双击才能打开显示菜单。如何避免双击。 onFocus 仍必须触发
我做了 float 文本输入,当用户专注于输入时,我的文本会 float ,但当用户点击我的输入时,我的输入中也需要一个“关闭 x”图标。 To include "x cloase" icon I w
知道我的代码有什么问题吗?我的 onFocus 事件在 div 上不起作用 class Hello extends React.Component { state = { openDrop
我希望按钮在通过 tab 键选择时具有发光效果。谁能帮我这个。代码片段将非常有帮助。 谢谢拉什米 最佳答案 如果您正在为现代浏览器开发,您可以使用 html5 框阴影和过渡: button{ bac
假设我有一个文本框的 onFocus 事件。正如预期的那样,当用户跳入该框时会触发该事件。但它似乎也像选择框时触发事件,然后通过切换选项卡,打开然后关闭另一个应用程序等覆盖然后打开窗口。有没有办法使事
与其他人一样,我们的网络应用程序中到处都有表单。 到目前为止,我们一直在大多数表单元素中使用以下(或一些变体): class="formThingy" onFocus="document.the
我想在我的注册表单上制作这种 Pinterest 效果 - 当用户关注输入文本字段或文本区域时,我希望该字段或区域周围有柔和的阴影,如下所示: 所以我尝试了以下 - 我写了一个 CSS 类 .inpu
Fiddle 所以我遇到了这个问题。 我有一个简单的表格: 还有这个脚本: function focus(element) { console.log('focused ' + el
我正在尝试在用户单击字段时向表单字段添加颜色边框,我擅长 html 和 javascript,还有一点 php,但我的 css 实际上很差。表单等的代码如下。如果有人可以指导或帮助我,我将不胜感激。代
我正在使用带有 LinearLayoutManager 的 RecyclerView 来填充对象的 ArrayList。我需要的是当我将焦点从一个项目切换到另一个项目时,如何实现 onFocus 事件
我是一名优秀的程序员,十分优秀!