简体   繁体   English

当我 select 输入框时,如何将“您正在输入数字”放在输入框的正下方?

[英]How can i put "you are entering a number" right below the input box when i select input box?

CSS: CSS:

.Input { background-color:grey; }
.Input:focus { background-color: white; }

HTML: HTML:

Enter the first number:<br>
<br>
Enter the second number:<br>
<br>
Enter the third number:<br>
<br>
Enter the fourth number:<br>
<br>
Enter the fifth number:<br>
<br>
<br>
Submit
<h1></h1>

JavaScript: JavaScript:

function get_product_of_inputs() {
        var a = document.getElementById("a").value;
        var b = document.getElementById("b").value;
        var c = document.getElementById("c").value;
        var d = document.getElementById("d").value;
        var e = document.getElementById("e").value;
        var f = a*b*c*d*e;
        console.log(f);
        document.getElementById("Submit").innerHTML= ("Product of all the numbers are "+f);
}

I am literally trying to put "you are entering a number" when someone select the input field.当某人 select 输入字段时,我实际上是在尝试输入“您正在输入数字”。 What can I do?我能做什么? Please help me out.请帮帮我。 I am a bit confused at this point?在这一点上我有点困惑?

use onfocus="yourjsfunc();"使用 onfocus="yourjsfunc();" in your html code.在您的 html 代码中。 For example:例如:

<input id="a" type="text" onfocus="yourjsfunc();">

And use display:none and display:block to hide and show your text-element.并使用 display:none 和 display:block 来隐藏和显示您的文本元素。

<form>
  <div>
    <label for="number">
      <input id="number" name="number">
      <span data-id="helper-text">You are entering a number</span>
    </label>
  </div>
  <div>
    <label for="foo">
      <input id="foo" name="foo">
      <span data-id="helper-text">You are entering foo</span>
    </label>
  </div>
</form>

CSS Approach CSS 进场

[data-id="helper-text"] {
  display: none;
}

label:focus-within [data-id="helper-text"] {
  display: inline-block;
}

JavaScript Approach JavaScript 进场

function changeHelperTextDisplay(target, style) {
  const helperTextEl = target
    .closest('label')
    ?.querySelector('[data-id="helper-text"]');
  if (helperTextEl) {
    helperTextEl.style.display = style;
  }
}

function handleFocus(event) {
  changeHelperTextDisplay(event.target, 'inline-block');
}

function handleBlur(event) {
  changeHelperTextDisplay(event.target, 'none');
}

const inputs = document.querySelectorAll('input');

inputs.forEach((input) => {
  input.addEventListener('focus', handleFocus);
  input.addEventListener('blur', handleBlur);
});

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

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