问题描述
- 我的项目中有一个模态,当单击按钮时弹出:
<div class="modal modal-visually-hidden">
<h3 class="modal__header">Оставить отзыв</h3>
<button name="Close the window" class="modal__close-button">x</button>
<div class="modal__form-container">
<form id="modal__form" action="#">
<label for="modal__form--name">Пожалуйста,заполните поле</label>
<input id="modal__form--name" type="text" placeholder="Имя" required> <!--this is the field needed to autofocus on modal open event-->
<input id="modal__form--qualities" type="text" placeholder="Достоинства">
<input id="modal__form--drawbacks" type="text" placeholder="Недостатки">
<input id="modal__form--submit" class="global-hover" type="submit" value="оставить отзыв">
</form>
</div>
- 这是弹出模式的按钮:
<div class="Feedback__button-container">
<button name="Leave a Feedback" id="Feedback__button" class="Feedback__button">оставить отзыв</button>
</div>
- 我使用以下JS来显示弹出窗口:
const activatePopupButton = document.querySelector('#Feedback__button');
const commentPopup = document.querySelector('.modal');
const closePopupButton = document.querySelector('.modal__close-button');
const nameInputField = document.querySelector('#modal__form--name'); /* I select the field for further autofocus */
closePopupButton.addEventListener('click',function() {
commentPopup.classList.add('modal-visually-hidden');
document.querySelector('body').style.overflow = 'visible';
});
activatePopupButton.addEventListener('click',function() {
commentPopup.classList.remove('modal-visually-hidden');
nameInputField.autofocus = true; /*this line should autofocus the field in question*/
console.log(nameInputField.autofocus);
document.querySelector('body').style.overflow = 'hidden';
});
使用样式为display:none;的类'.modal-visually-hidden'隐藏模块。 当按下按钮时,将显示弹出窗口,并且第一个字段应该与行名InputField.autofocus = true对齐。 但这不会。我在做什么错了?
解决方法
Tick
属性将输入集中在页面加载上。 DOM加载后无法设置。相反,请尝试以下操作:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Lock_Test_2
{
public partial class Form1 : Form
{
Timer timer1;
readonly object lockObj = new object();
public Form1()
{
InitializeComponent();
Button button1 = new Button();
button1.Location = new Point(100,100);
button1.Size = new Size(187,67);
button1.Text = "button1";
button1.Click += button1_Click;
Controls.Add(button1);
timer1 = new Timer();
timer1.Tick += timer1_Tick;
}
private void button1_Click(object sender,EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender,EventArgs e)
{
lock (lockObj)
{
MessageBox.Show("Entered the lock!");
MessageBox.Show("Exiting the lock...");
}
}
}
}