[英]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.