简体   繁体   English

在 Javascript 中对输入字段进行分组

[英]Grouping input fields in Javascript

I am trying to group my name/number/email field together so I can have one event listener.我正在尝试将我的姓名/号码/电子邮件字段组合在一起,以便我可以拥有一个事件侦听器。 I want to make it so when the user leaves the input field, it plays the audio.我想让它在用户离开输入字段时播放音频。 I have tried adding an event to the parent .infofield class but it doesn't work for onblur/focus events.我曾尝试向父.infofield类添加一个事件,但它不适用于 onblur/focus 事件。 I was wondering how I'd approach this in Javascript?我想知道如何在 Javascript 中处理这个问题?

        <section class="infofield">
            <audio id="leave" src= "blur.mp3"></audio>

            <section>
                <p class ="name">Name*</p>
                First: <br><input type="text" name="fname" required><br/>
                Last: <br><input type="text" name="lname" required><br/>
            </section>

            <section>
                <p class ="name">Phone Number*</p>
                <input type="text" name="number" required>
            </section>

            <section>
                <p>Email*</p>
                <input type="email" name="email" required>  
            </section>

        </section>

I do not see any difficulties, what is the problem?我看不出有什么困难,有什么问题?

 const myForm = document.getElementById('my-form') ; myForm.in_fields.querySelectorAll('input').forEach(inElm=> { inElm.onfocus=focusOn; inElm.onblur=focusOff; }) function focusOn(e) { WiGo.textContent = `evt focus on ${e.target.name}` } function focusOff(e) { WiGo.textContent = `evt blur on ${e.target.name}` } /* previous version -------------------------------------------------- myForm.in_fields.onmouseover=e=> { WiGo.textContent = 'mouse is over Personal informations fields' } myForm.in_fields.onmouseout=e=> { WiGo.textContent = 'mouse is out of Personal informations fields' } ----------------------------------------------------------------------*/
 #my-form{ font-size: 14px; font-family: Arial, Helvetica, sans-serif; } fieldset { width:22em; padding: 0 1em; } legend::before, legend::after { content: '\\00a0' } label,input { float: left; margin: .4em; } input { width:11em ; padding: .2em; border-width: 1px; padding: .3em 5px; } label { display: block; clear: both; width: 9em; text-align: right; line-height: 1.5em; } label::after { content: '* :' }
 <form id="my-form"> <!--audio id="leave" src= "blur.mp3"></audio --> <fieldset name="in_fields"> <legend>Personal informations</legend> <label> First Name</label> <input type="text" name="fname" required> <label>Last Name</label> <input type="text" name="lname" required> <label>Phone Number</label> <input type="text" name="number" required> <label>Email</label> <input type="email" name="email" required> </fieldset> <p id="WiGo" >What's going on ?</p> </form>

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

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