聚焦时向输入添加类,不聚焦时将其删除

Posted

技术标签:

【中文标题】聚焦时向输入添加类,不聚焦时将其删除【英文标题】:Add class to an input when its focused, and remove it when isnt focused 【发布时间】:2022-01-15 14:15:23 【问题描述】:

我正在尝试将“.contact__form-input--focused”类添加到从表单获得焦点的输入中。

我正在尝试为每个输入添加一个事件侦听器,然后如果该类已经从类列表中删除该类。

//INPUT ANIMATION
const input = document.querySelectorAll("contact__form-input");

function addClass(input) 
  input.classList.add("contact__form-input--focused");


function removeClass(input) 
  input.classList.remove("contact__form-input--focused");


for (let i = 0; i < input.length; i++) 
  if (item[i].classList.contains("contact__form-input--focused")) 
    item.addEventListener("focus", addClass(input[i]));
   else 
    item.addEventListener("blur", removeClass(input[i]));
  
.contact__form 
  display: flex;
  flex-direction: column;


.contact__form-input 
  border: none;
  border-bottom: .1rem solid rgba(0, 0, 0, .12);
  font-size: var(--medium-font-size);
  margin: 0;
  padding: 4px 0;
  width: 100%;
  background: 0 0;
  text-align: left;
  color: inherit;


.contact__form-input--focused 
  /*some animations here*/
<form class="contact__form" method="POST">
  <label class="contact__form-label">
            <span>Name</span>
            <input class="contact__form-input" name="Name" type="text" autocomplete="name" required>
          </label>
  <label class="contact__form-label">
            <span>Phone number</span>
            <input class="contact__form-input" name="Phone number" type="tel" autocomplete="tel" required>
          </label>
  <label class="contact__form-label">
            <span>Message</span>
            <input class="contact__form-input" type="text" required>
          </label>
  <button class="contact__form-button">Send</button>
</form>

【问题讨论】:

为什么不直接使用:focus CSS pseudo-class ? 【参考方案1】:

如果你使用这样的 JS 而不仅仅是 :focus 的伪代码,你可以这样做

const inputs = document.querySelectorAll("contact__form-input");
inputs.forEach(input => 
    input.addEventListener("focus", function()
        this.classList.add("contact__form-input--focused");
    );
    input.addEventListener("blur", function()
        this.classList.remove("contact__form-input--focused");
    );
);

或者如果你想切换课程

const inputs = document.querySelectorAll("contact__form-input");
function switchInputClasses()
    this.classList.toggle("contact__form-input");
    this.classList.toggle("contact__form-input--focused");

inputs.forEach(input => 
    input.addEventListener("focus", switchInputClasses);
    input.addEventListener("blur", switchInputClasses);
);

函数声明而不是()=&gt; 的原因是因为后者将保留作用域并正确设置this 关键字,您需要更改作用域,最好的方法是EventListner通过元素上的apply() 调用

伪方法只是将您的 CSS 更改为

.contact__form 
  display: flex;
  flex-direction: column;


.contact__form-input 
  border: none;
  border-bottom: .1rem solid rgba(0, 0, 0, .12);
  font-size: var(--medium-font-size);
  margin: 0;
  padding: 4px 0;
  width: 100%;
  background: 0 0;
  text-align: left;
  color: inherit;


.contact__form-input:focus 
  /*some animations here*/

【讨论】:

【参考方案2】:

1) 你没有得到 html 元素,因为 contact__form-input 是一个类,你必须告诉 querySelectorAll 你正在寻找类是 contact__form-input 的所有元素,方法是附加 @ 987654324@ 在类名之前

const inputs = document.querySelectorAll( ".contact__form-input" );

2) 您应该在focusblur 上添加eventListener 为:

inputs.forEach(input => 
  input.addEventListener("focus", () => addClass(input))
  input.addEventListener("blur", () => removeClass(input))
)

NOTE: For demo purpose I've added thick red border

//INPUT ANIMATION
const inputs = document.querySelectorAll(".contact__form-input");

function addClass(input) 
  input.classList.add("contact__form-input--focused");


function removeClass(input) 
  input.classList.remove("contact__form-input--focused");


console.log(inputs)

inputs.forEach(input => 
  input.addEventListener("focus", () => addClass(input))
  input.addEventListener("blur", () => removeClass(input))
)
.contact__form 
  display: flex;
  flex-direction: column;


.contact__form-input 
  border: none;
  border-bottom: .1rem solid rgba(0, 0, 0, .12);
  font-size: var(--medium-font-size);
  margin: 0;
  padding: 4px 0;
  width: 100%;
  background: 0 0;
  text-align: left;
  color: inherit;


.contact__form-input--focused 
  /*some animations here*/
  border: 5px solid red;
<form class="contact__form" method="POST">
  <label class="contact__form-label">
            <span>Name</span>
            <input class="contact__form-input" name="Name" type="text" autocomplete="name" required>
          </label>
  <label class="contact__form-label">
            <span>Phone number</span>
            <input class="contact__form-input" name="Phone number" type="tel" autocomplete="tel" required>
          </label>
  <label class="contact__form-label">
            <span>Message</span>
            <input class="contact__form-input" type="text" required>
          </label>
  <button class="contact__form-button">Send</button>
</form>

【讨论】:

【参考方案3】:

您不需要if 语句。只需将两个事件侦听器添加到所有输入。

addEventListener() 的第二个参数必须是函数。您是立即调用函数,而不是在事件发生时。

//INPUT ANIMATION
const input = document.querySelectorAll(".contact__form-input");

function addClass(input) 
  input.classList.add("contact__form-input--focused");


function removeClass(input) 
  input.classList.remove("contact__form-input--focused");


for (let i = 0; i < input.length; i++) 
  let item = input[i];
  item.addEventListener("focus", () => addClass(item));
  item.addEventListener("blur", () => removeClass(item));
.contact__form 
  display: flex;
  flex-direction: column;


.contact__form-input 
  border: none;
  border-bottom: .1rem solid rgba(0, 0, 0, .12);
  font-size: var(--medium-font-size);
  margin: 0;
  padding: 4px 0;
  width: 100%;
  background: 0 0;
  text-align: left;
  color: inherit;


.contact__form-input--focused 
  background-color: yellow;
<form class="contact__form" method="POST">
  <label class="contact__form-label">
            <span>Name</span>
            <input class="contact__form-input" name="Name" type="text" autocomplete="name" required>
          </label>
  <label class="contact__form-label">
            <span>Phone number</span>
            <input class="contact__form-input" name="Phone number" type="tel" autocomplete="tel" required>
          </label>
  <label class="contact__form-label">
            <span>Message</span>
            <input class="contact__form-input" type="text" required>
          </label>
  <button class="contact__form-button">Send</button>
</form>

【讨论】:

querySelectorAll 中选择没有.。语句不应该是document.querySelectorAll( ".contact__form-input" );【参考方案4】:
<style type="text/css">
    .contact__form 
        display: flex;
        flex-direction: column;
    
    .contact__form-input 
        border: none;
        border-bottom: .1rem solid rgba(0, 0, 0, .12);
        font-size: var(--medium-font-size);
        margin: 0;
        padding: 4px 0;
        width: 100%;
        background: 0 0;
        text-align: left;
        color: inherit;
    

    .contact__form-input--focused 
        /*some animations here*/
    
</style>

<form class="contact__form" method="POST">
    <label class="contact__form-label">
        <span>Name</span>
        <input class="contact__form-input" name="Name" type="text" autocomplete="name">
    </label>
    <label class="contact__form-label">
        <span>Phone number</span>
        <input class="contact__form-input" name="Phone" type="tel" autocomplete="tel" required>
    </label>
    <label class="contact__form-label">
        <span>Message</span>
        <input class="contact__form-input" name="Text" type="Text" required>
    </label>
    <button class="contact__form-button">Send</button>
</form>

<script>
    const inputName = document.querySelector("input[name=Name]");
    const inputPhone = document.querySelector("input[name=Phone]");
    const inputText = document.querySelector("input[name=Text]");
    inputName.addEventListener("focus", function() 
        if(inputName.classList.contains("contact__form-input--focused"))
            inputName.classList.remove("contact__form-input--focused");
        else
            inputName.classList.add("contact__form-input--focused");
    );
    inputPhone.addEventListener("focus", function() 
        if(inputPhone.classList.contains("contact__form-input--focused"))
            inputPhone.classList.remove("contact__form-input--focused");
        else
            inputPhone.classList.add("contact__form-input--focused");
    );
    inputText.addEventListener("focus", function() 
        if(inputText.classList.contains("contact__form-input--focused"))
            inputText.classList.remove("contact__form-input--focused");
        else
            inputText.classList.add("contact__form-input--focused");
    );
</script>

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于聚焦时向输入添加类,不聚焦时将其删除的主要内容,如果未能解决你的问题,请参考以下文章

将可点击按钮添加到输入而不“聚焦”输入

模糊后无法聚焦回输入字段

javascript JQuery - 输入聚焦/模糊类切换

jQuery-添加/聚焦/模糊输入值

js元素聚焦(vue)

打印焦点输入元素时,Bootstrap 显示黑色矩形