简体   繁体   English

为什么在 mouseout 事件处理程序中将 input type="number" 设置为 type="text" 不起作用?

[英]Why doesn't setting an input type="number" to type="text" in an mouseout event handler working?

Goal:目标:

I have an input element in which the user should enter a number between 1 and 999, optionally may use the (input type="number") up/down spinner, but when the mouse isn't over this element or the element doesn't have the focus, I want the input element to be of 'text' type to ensure that the input is sized to only show the element's value without the spinner showing or the blank area of the hidden spinner.我有一个输入元素,用户应该在其中输入一个介于 1 和 999 之间的数字,可以选择使用(输入类型=“数字”)向上/向下微调器,但是当鼠标不在这个元素上或者元素没有'如果没有焦点,我希望输入元素为“文本”类型,以确保输入的大小仅显示元素的值,而不显示微调器或隐藏微调器的空白区域。 This way the value in the input element and the text to the right of the element doesn't move whether the element is of type 'text' or 'number', the element has the focus or not, or the mouse is over the element or not.这样,输入元素中的值和元素右侧的文本不会移动,无论元素是“文本”还是“数字”类型,元素是否具有焦点,或者鼠标悬停在元素上或不。

Background:背景:

Currently, I'm initially setting the input element's type to 'text', margin-left to '2ch', and width to '4ch', then when the mouse is over the element, I set the element's type to 'number', margin-left to '0', and width to '6ch'.目前,我最初将输入元素的类型设置为“文本”,左边距设置为“2ch”,宽度设置为“4ch”,然后当鼠标悬停在元素上时,我将元素的类型设置为“数字”,左边距为“0”,宽度为“6ch”。 When the mouse moves off of the element or the element looses focus, I set these properties back to their initial values.当鼠标离开元素或元素失去焦点时,我将这些属性设置回它们的初始值。

Code:代码:

<style>
  /* Number text (1/3 etc) */
  .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: -90px; }
</style>
<div class="numbertext">
  <input type="text" style="text-align: right; margin-left: 2ch; width: 4ch;" min="1" max="999" value="1"
         onmouseover="setInputType( 'number' );"
         mouseout="setInputType( 'text' );"
         onblur="setInputType( 'text' );" />/
  <span>999</span>
</div>
<script>
  function setInputType( type ) {
    var input = window.event.currentTarget;

    if( type === 'text' ) {

      input.style.marginLeft = '2ch'; 
      input.style.width      = '4ch';

    }
    else {

      input.style.marginLeft = '0'; 
      input.style.width      = '6ch';

    }
    input.type = type;
  }
</script>

Problem:问题:

When the page initially shows, the code displays the way that I want, hovering the cursor over the field or focusing on it works, too.当页面最初显示时,代码以我想要的方式显示,将 cursor 悬停在该字段上或关注它也可以。 However, moving the mouse away from the field after the mouse hovered over the it, or the field looses the focus, inconsistently restores the initial setting of the input element because the mouseout and blur event handlers aren't always triggering.但是,在鼠标悬停在该字段上或该字段失去焦点后将鼠标从该字段上移开,会不一致地恢复输入元素的初始设置,因为 mouseout 和 blur 事件处理程序并不总是触发。 I know this because putting a breakpoint on the setInputType function's if( type === 'text' ) statement branch and running the code in the Chrome inspect > source panel doesn't stop the code execution when the mouse moves away from the element after it mouse was moved over it.我知道这是因为在 setInputType 函数的if( type === 'text' )语句分支上放置一个断点并在 Chrome 检查 > 源面板中运行代码不会在鼠标从元素后移开时停止代码执行它鼠标移到它上面。

Any ideas about why the mouseout and blur event handlers aren't properly working consistently?关于为什么 mouseout 和 blur 事件处理程序不能正常工作的任何想法?

Solution:解决方案:

This CodePage shows a fully working solution that includes Bryan Elliott's correction and Jon P's suggestion.代码页显示了一个完整的解决方案,其中包括 Bryan Elliott 的更正和 Jon P 的建议。

Thanks谢谢

On your element events, you have mouseout="..." , It needs to be: onmouseout="..."在你的元素事件上,你有mouseout="..." ,它需要是: onmouseout="..."

Like this:像这样:

<input
    type="text"
    style="text-align: right; margin-left: 2ch; width: 4ch;"
    min="1" max="999"
    value="1"
    onmouseover="setInputType( 'number' );"
    onmouseout="setInputType( 'text' );"   //notice the onmouseout=""  instead of mouseout=""
    onblur="setInputType( 'text' );"
/>

Use event listeners to bind the events unobtrusively.使用事件侦听器以不显眼的方式绑定事件。 Then use event.type to determine what action to take.然后使用event.type来确定要采取的操作。

 //Get the relevent elements let numText = document.querySelectorAll(".numbertext input[type='text']"); //Loop them for (var i = 0; i < numText.length; i++) { //Now loop the event types we're interested in "blur focus mouseenter mouseleave".split(" ").forEach(function(e) { //and add the event listener for that event type numText[i].addEventListener(e, setInputType); }) } //Event listener function setInputType(event) { var type = "text"; //Dont change on mouse leave if the element has focus if(event.type == "blur" || (event.type == "mouseleave" && this.= document.activeElement )) { this.style;marginLeft = '2ch'. this.style;width = '4ch'; type = "text". } else { this.style;marginLeft = '0'. this.style;width = '6ch'; type = "number". } this;type = type ; }
 .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; /*position: absolute; top: -90px;*/ }
 <div class="numbertext"> <input type="text" style="text-align: right; margin-left: 2ch; width: 4ch;" min="1" max="999" value="1" />/ <span>999</span> </div> <div class="numbertext"> <input type="text" style="text-align: right; margin-left: 2ch; width: 4ch;" min="1" max="999" value="1" />/ <span>999</span> </div>

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

相关问题 为什么输入事件不能与隐藏的输入类型一起使用? - Why input event not working with input type hidden? 单选按钮的 OnChange 事件处理程序 (INPUT type="radio") 不能作为一个值工作 - OnChange event handler for radio button (INPUT type="radio") doesn't work as one value html<input type="text"> onchange 事件不起作用 - html <input type="text" /> onchange event not working 输入 [type=&quot;number&quot;] 处理程序无法正常工作 [Formik, RegExp] - Input[type="number"] handler not working properly [Formik, RegExp] 当我在呈现视图后设置该输入类型的值时,为什么不会触发输入类型的更改事件? - Why wouldn't the change event of a input type fire when I'm setting the value of that input type after the view is rendered? javascript mouseout事件处理程序功能不起作用 - javascript mouseout event handler function not working 为什么Leaflet在mouseout事件上不提供正确的relatedTarget元素? - Why doesn't Leaflet provide the correct relatedTarget element on mouseout event? 输入类型=数字的设置步骤 - setting steps for input type=number 为什么iPhone的Safari触发器不会在输入类型=文件上更改事件? - Why doesn't iPhone's Safari trigger changed event on input type=file? 在jQuery事件处理程序中检查input [type = checkbox] - Check input[type=checkbox] in jQuery event handler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM