如何运行这个html组合框样例
How to run this html combobox sample?
大家好,我一直在寻找一个简单的例子,如何使html选择看起来像一个组合框。我从这个链接中找到了一个示例代码片段。这实际上是对一个类似于我的问题的回答。它的答案是Samich
。它有一个简洁的链接。我从jsfiddle中复制了代码,并稍微调整了一下,使它能在我的机器上运行,但我不能使它运行。
下面是代码(大部分代码是由Samich编写的):
<html>
<head>
<style type="text/css">
#dropdown { position:absolute; width:200px; display:none; }
#dropdown li:hover { background:#ccc; cursor:pointer; }
</style>
<script type="text/javascript" src="jquery1.6.4min.js"></script>
<script type="text/javascript" >
$('#btn').click(function() {
var pos = $('#txt').offset();
pos.top += $('#txt').width();
$('#dropdown').fadeIn(100);
return false;
});
$('#dropdown li').click(function() {
$('#txt').val($(this).text());
$(this).parent().fadeOut(100);
});
</script>
</head>
<body>
<input type="text" id="txt" /><a href="#" id="btn">V</a>
<ul id="dropdown">
<li>Value 1</li>
<li>Value 2</li>
<li>Value 3</li>
<li>Value 4</li>
<li>Value 5</li>
<li>Value 6</li>
<li>Value 7</li>
<li>Value 8</li>
<li>Value 9</li>
<li>Value 10</li>
<li>Value 11</li>
<li>Value 12</li>
</ul>
</body>
</html>
我是非常新的javascript和jquery所以请忍受我的家伙。
<script type="text/javascript">
$(document).ready(function() {
$('#btn').click(function() {
var pos = $('#txt').offset();
pos.top += $('#txt').width();
$('#dropdown').fadeIn(100);
return false;
});
$('#dropdown li').click(function() {
$('#txt').val($(this).text());
$(this).parent().fadeOut(100);
});
});
</script>
差不多了!您需要确保将事件附加到元素的脚本在浏览器创建元素之后运行,只需使用ready()函数;
$(document).ready(function() {
$('#btn').click(function() {
var pos = $('#txt').offset();
pos.top += $('#txt').width();
$('#dropdown').fadeIn(100);
return false;
});
$('#dropdown li').click(function() {
$('#txt').val($(this).text());
$(this).parent().fadeOut(100);
});
});
为什么不使用作为jQuery UI一部分的comboBox呢?
如果您使用<a href="#" id="btn">V</a>
则使用do $('btn')
而不是$('#btn')
…
相关文章:
- 如何将具有相同功能的两个select html标签的两个JS组合在一起
- 通过Knockout组合文本和html
- 如何使用php和html根据文本框中的输入值自动更改组合框值
- 有人可以告诉我如何调试这个 html 表单/javascript 组合
- 通过参数设置html组合框的选定选项
- 将多字段html输入组合为一个参数并传递给javascript
- html+css+js组合在Google Chrome或Safari浏览器中无法正常工作
- 如何在可视化web开发中将javascript组合到html/ASP.Net中
- Angular.js & HTML / 如何并排组合 2 个元素
- 如何根据 html 页面中组合框中的值填充文本框的值
- 在 html “onclick” 事件中组合许多类似的操作
- 使用 JavaScript 组合 HTML 段落
- 如何在 HTML 中组合 JavaScript 变量
- 如何获取选定的组合框值并以 html 形式加载其相应的数据
- HTML组合框所选项目's值
- JavaScript使用Html组合框填充输入字段
- 如何运行这个html组合框样例
- 我可以使用纯JavaScript(没有html组合)从控制台读取值吗?(如Java和c++)
- 使用Javascript/JQuery的HTML组合框
- 确定HTML组合框是展开还是折叠