focus()不适用于选择框(下拉列表)

时间:2014-04-04 17:21:47

标签: javascript jquery

在某些特定事件中,我想专注于特定的选择框。但是

document.getElementById('myselect').focus()

无效。它适用于文本输入,但不适用于选择框。

2 个答案:

答案 0 :(得分:0)

它应该工作并且正常工作..检查fiddle

- HTML -

<select id="dpk">
    <option> first </option>
    <option> second </option>
    <option> third </option>
</select>
<input type="button" value="focus Dropdown" id="btn" onclick="my_method()" />

- javscript ---

function my_method()
{
    document.getElementById('dpk').focus();
}

答案 1 :(得分:0)

你确定没有聚焦吗?

我刚刚使用以下代码对jsFiddle进行了快速测试 - jsFiddle

<select name="dropdown" id="dropdown">
    <option value="1">One</option>
    <option value="2">two</option>
</select>

<input type="text" id="textfield" /><br />

    <button id="clickme" 
         onclick="javascript: document.getElementById('dropdown').focus()">
            focus dropdown</button>
    <button id="clickme1" 
         onclick="javascript: document.getElementById('textfield').focus()">
        focus textfield</button>

 Pressing the "Focus Dropdown" button does set the focus to the drop down field, but it doesn't actually appear to do anything.  The dropdown field isn't highlighted, although if I press the arrow keys, different values will be selected.