简体   繁体   English

如何在javascript中自动展开html选择元素

[英]How to automatically expand html select element in javascript

I have a (hidden) html select object in my menu attached to a menu button link, so that clicking the link shows the list so you can pick from it.我的菜单中有一个(隐藏的)html 选择对象附加到菜单按钮链接,因此单击该链接会显示列表,以便您可以从中进行选择。

When you click the button, it calls some javascript to show the <select> .当您单击按钮时,它会调用一些 javascript 来显示<select> Clicking away from the <select> hides the list.单击远离<select>隐藏列表。 What I really want is to make the <select> appear fully expanded, as if you had clicked on the "down" arrow, but I can't get this working.我真正想要的是使<select>显示完全展开,就好像您单击了“向下”箭头一样,但我无法使其正常工作。 I've tried lots of different approaches, but can't make any headway.我尝试了很多不同的方法,但无法取得任何进展。 What I'm doing currently is this:我目前正在做的是:

<li>
    <a href="javascript:showlist();"><img src="/images/icons/add.png"/>Add favourite</a>
    <select id="list" style="display:none; onblur="javascript:cancellist()">
    </select>
</li>

// in code
function showlist() {
    //using prototype not jQuery
    $('list').show();  // shows the select list
    $('list').focus(); // sets focus so that when you click away it calles onblur()
}
  • I've tried calling $('list').click() .我试过调用$('list').click()
  • I've tried setting onfocus="this.click()" But in both cases I'm getting我试过设置onfocus="this.click()"但在这两种情况下我都得到

Uncaught TypeError: Object # has no method 'click'未捕获的类型错误:对象 # 没有方法“单击”

which is peculiar as link text says that it supports the standard functions.这很特别,因为链接文本说它支持标准功能。

I've tried setting the .size = .length which works, but doesn't have the same appearance (as when you click to open the element, it floats over the rest of the page.)我试过设置.size = .length有效,但没有相同的外观(当你点击打开元素时,它漂浮在页面的其余部分。)

Does anyone have any suggestions?有没有人有什么建议?

I've come across the same problem, wanted to implement keyboard navigation in my app, but select elements were not expanded, so people using the keyboard were going to lose functionality.我遇到了同样的问题,想在我的应用程序中实现键盘导航,但选择元素没有展开,所以使用键盘的人将失去功能。 I've created ExpandSelect() function which emulates mouse clicks on select elements, it does so by creating another <select> with multiple options visible at once by setting the size attribute, the code is hosted on Google Code website, ExpandSelect.js is only 4 KB, see screenshots and download it here:我创建了 ExpandSelect() 函数来模拟鼠标在选择元素上的点击,它通过创建另一个具有多个选项的<select>来实现,通过设置size属性,代码托管在 Google Code 网站上,ExpandSelect.js 是只有 4 KB,请查看屏幕截图并在此处下载:

http://code.google.com/p/expandselect/ http://code.google.com/p/expandselect/

Screenshots截图

There is a little difference in GUI when emulating click, but it does not really matter, see it for yourself:模拟点击的时候GUI有一点区别,但其实无所谓,自己看吧:

When mouse clicking:鼠标点击时:

鼠标点击
(source: googlecode.com ) (来源: googlecode.com

When emulating click:模拟点击时:

模拟点击
(source: googlecode.com ) (来源: googlecode.com

More screenshots on project's website, link above.项目网站上的更多屏幕截图,上面的链接。

Here a simple solution, AUTO-EXPANDED select menu (all options visible)这是一个简单的解决方案,AUTO-EXPANDED 选择菜单(所有选项可见)

<select name="ddlTestSelect" id="ddlTestSelect" style="width:200px;position:absolute;">
  <option selected="selected" value="0">defaulttt</option>
  <option>option 2</option>
  <option>option 3</option>
  <option>option 4</option>
</select>
<script type="text/javascript">
 var slctt=document.getElementById("ddlTestSelect");
 slctt.size=slctt.options.length;if(slctt.options.length>1)slctt.style.width='470px';
</script>

Please try this:请试试这个:

function showlist() {
    //using prototype not jQuery
    $('#list').show();  // shows the select list
    $('#list').focus(); // sets focus so that when you click away it calles onblur()
}

You have a syntax error.您有语法错误。 It should be:它应该是:

$('#list').click();

You forgot the #你忘记了#

The simplest way is to use the "multiple" attribute in HTML最简单的方法是使用 HTML 中的“multiple”属性

<select multiple></select>

and you could also add a style attribute to set the height of the list你也可以添加一个样式属性来设置列表的高度

<select multiple style="height:150px;"></select>

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

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