简体   繁体   English

聚焦选择元素的最近距离

[英]Focus Nearest Div of Select Element

I have select element 我有选择元素

  <select id="tokenize" multiple="multiple" 
class="tokenize" style="margin: 0px;
 padding: 0px; border: 0px; display: none;"/>

Under select element there is div with it is class 在select元素下有div,它是class

<div class="tokenize Tokenize">
<ul class="TokensContainer ui-sortable">
<li class="Placeholder" style="display: list-item;">
enter Position</li><li class="TokenSearch">
<input size="5"></li></ul><ul class="Dropdown"
 style="display: none;"></ul></div>

I want to focus(with cursor) that div which is nearest to select.That div created dynamically via javascript so I can not fix it from html so I try javascript.I want to focus div(it is class tokenize) which is nearest select element(it is id tokenize) 我想聚焦(用光标)最接近选择的那个div。那个div是通过javascript动态创建的,所以我不能从html修复它,所以我尝试javascript。我想聚焦最接近选择的div(它是类标记化)元素(它是id标记化)

I try this but this not work 我尝试这个,但这不起作用

  document.getElementById(Select-Element-ID).firstElementChild.focus().select();

You have to add the tabindex attribute to the div to make it focusable. 您必须将tabindex属性添加到div以使其具有焦点。
Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex 资料来源: https : //developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex

UPDATE: 更新:

var $inputToFocus = document.getElementById(Select-Element-ID).nextElementSibling.querySelector('.TokenSearch input');

$inputToFocus.focus();

This is the most advanced solution of your problem using Jquery.. and yet very simple 这是使用Jquery解决问题的最高级方法。

$("#tokenize").closest("div").focus(); $(“#tokenize”)。closest(“ div”)。focus();

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

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