你可以使用activeElement属性来查看具有特定类名的元素是否具有焦点。

huangapple go评论63阅读模式
英文:

Can you use the activeElement property to see if an element with a specific class name has focus?

问题

我正在尝试在具有特定类名的元素获得布尔值 true,如果它们处于焦点状态。但是似乎 Document.activeElement 仅适用于 ID 或标签名称,这对于它们都是相同类型的元素没有帮助。以下是情况:

<!-- 开始片段: js 隐藏: false 控制台: false babel: false -->

<!-- 语言: lang-js -->
var test2 = document.getElementsByClassName('test2')
var test3 = document.getElementsByClassName('test3')

test3[1].focus()

isFocused2 = (document.activeElement === test2)
isFocused3 = (document.activeElement === test3)

document.getElementById('test').innerHTML = isFocused3

<!-- 语言: lang-html -->
<input type="text" class="test2">
<input type="text" class="test3">
<input type="text" class="test2">
<input type="text" class="test3">

<div id="test"></div>

<!-- 结束片段 -->

@Michael 只有在将 focus () 设置为输入时才有效,如下所示,但似乎点击输入框不会使其处于活动状态,因此 isFocused 变量保持为 false(我误导您,使您认为我想使用该方法来聚焦而不是单击):

<!-- 开始片段: js 隐藏: false 控制台: false babel: false -->

<!-- 语言: lang-html -->
<table>
        <tr>
   <td><input type="text" name="" class="prev" onclick="fx2()"></td>
   <td><input type="text" name="" class="curr" onclick="fx2()"></td>
   <td><p class="Mtr-result"></p></td>
        </tr>
        <tr>
    <td><input type="text" name="" class="prev" onclick="fx2()"></td>
    <td><input type="text" name="" class="curr" onclick="fx2()"></td>
    <td><p class="Mtr-result"></p></td>
        </tr>


    <div  id="test"></div>

<!-- 语言: lang-js -->
const prev = document.getElementsByClassName('prev');
const curr = document.getElementsByClassName('curr');

// curr[1].focus()

var isFocused1 = document.activeElement.classList.contains("prev");
var isFocused2 = document.activeElement.classList.contains("curr");


function fx2 () {
    document.getElementById('test').innerHTML += isFocused1 + ' ' + isFocused2 + ' '
}

<!-- 结束片段 -->

如您所见,聚焦方法已被注释掉,输入框上的 onclick 事件属性调用 fx2() 函数,该函数更改了 'test' div 的 innerHTML 值为 isFocused 1 和 2 的值,它们都显示为 false。

英文:

Im trying to get the Boolean true if elements with a certain class name are focused on. But it seems Document.activeElement only works with either ID or Tag name, which wont help since they're elements of the same type. Heres the situation:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

var test2 = document.getElementsByClassName(&#39;test2&#39;)
var test3 = document.getElementsByClassName(&#39;test3&#39;)

test3[1].focus()

isFocused2 = (document.activeElement === test2)
isFocused3 = (document.activeElement === test3)

document.getElementById(&#39;test&#39;).innerHTML = isFocused3

<!-- language: lang-html -->

&lt;input type=&quot;text&quot; class=&quot;test2&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;test3&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;test2&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;test3&quot;&gt;

&lt;div id=&quot;test&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

@Michael It works ONLY if the focus () is set to the input as you'll see below, but clicking on the input doesn't seem to make it active hence making the isFocused variable stay on false (my mistake in giving you the idea that i wanted to use that method to focus instead of clicking)

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

<table>
<tr>
<td><input type="text" name="" class="prev" onclick="fx2()"></td>
<td><input type="text" name="" class="curr" onclick="fx2()"></td>
<td><p class="Mtr-result"></p></td>
</tr>
<tr>
<td><input type="text" name="" class="prev" onclick="fx2()"></td>
<td><input type="text" name="" class="curr" onclick="fx2()"></td>
<td><p class="Mtr-result"></p></td>
</tr>

&lt;div  id=&quot;test&quot;&gt;&lt;/div&gt;

<!-- language: lang-js -->

const prev = document.getElementsByClassName('prev');
const curr = document.getElementsByClassName('curr');

// curr[1].focus()

var isFocused1 = document.activeElement.classList.contains("prev");
var isFocused2 = document.activeElement.classList.contains("curr");

function fx2 () {
document.getElementById('test').innerHTML += isFocused1 + ' ' + isFocused2 + ' '
}

<!-- end snippet -->

as you can see the focus method has been commented out, and the onclick event attribute on the input calls the fx2() function that changes the innerHTML value of the 'test' div to the value of isFocused 1 and 2, which both show false.

答案1

得分: 1

document.activeElement 正常运行 🙂:

const test3 = document.querySelector('.test3');

document.querySelectorAll('input').forEach(el => {
  el.onfocus = state;
  el.onblur = state;
  function state() {
    document.getElementById('active-focus-class').innerHTML = document.activeElement.getAttribute('class');
    document.getElementById('test').innerHTML = document.activeElement === test3;
  }
})

test3.focus();
<input type="text" class="test1">
<input type="text" class="test2">
<input type="text" class="test3">
<input type="text" class="test4">

<p>.test3 is focused: <b id="test"></b></p>
<p>activeElement className: <b id="active-focus-class"></b></p>
英文:

document.activeElement works fine 🙂:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const test3 = document.querySelector(&#39;.test3&#39;);

document.querySelectorAll(&#39;input&#39;).forEach(el =&gt; {
  el.onfocus = state;
  el.onblur = state;
  function state() {
    document.getElementById(&#39;active-focus-class&#39;).innerHTML = document.activeElement.getAttribute(&#39;class&#39;);
    document.getElementById(&#39;test&#39;).innerHTML = document.activeElement === test3;
  }
})

test3.focus();

<!-- language: lang-html -->

&lt;input type=&quot;text&quot; class=&quot;test1&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;test2&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;test3&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;test4&quot;&gt;

&lt;p&gt;.test3 is focused: &lt;b id=&quot;test&quot;&gt;&lt;/b&gt;
&lt;p&gt;activeElement className: &lt;b id=&quot;active-focus-class&quot;&gt;&lt;/b&gt;

<!-- end snippet -->

答案2

得分: 0

正如Bergi和Zachiah在评论中指出的,你只需要使用 document.activeElement.classList.contains("test2") 来检查焦点元素是否具有 test2 类。

Element.classList.contains(class) 是一个你可以在任何元素上调用的方法,包括 document.activeElement。如果你调用它的元素包含传递给该方法的类,则它将返回 true,否则将返回 false

英文:

As Bergi and Zachiah pointed out in the comments, all you need to do is use document.activeElement.classList.contains(&quot;test2&quot;) to check if the focused element has the test2 class.

Element.classList.contains(class) is a method that you can call on any element, including document.activeElement. If the element you're calling it contains the class passed to the method, then it will return true, otherwise it will return false.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var test2 = document.getElementsByClassName(&#39;test2&#39;)
var test3 = document.getElementsByClassName(&#39;test3&#39;)

var isFocused2;
var isFocused3;

function fx() {
  isFocused2 = document.activeElement.classList.contains(&quot;test2&quot;)
  isFocused3 = document.activeElement.classList.contains(&quot;test3&quot;)

  document.getElementById(&#39;test&#39;).innerHTML = isFocused2 + &#39; &#39; + isFocused3;
}

window.addEventListener(&#39;focusin&#39;, fx);
window.addEventListener(&#39;focusout&#39;, fx);

<!-- language: lang-html -->

&lt;input type=&quot;text&quot; class=&quot;test2&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;test3&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;test2&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;test3&quot;&gt;

&lt;div id=&quot;test&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月28日 03:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定