简体   繁体   English

我可以从HTML元素获取到已经存在的JS元素

[英]Can I Get From HTML Element to Already Existing JS Element

Background 背景

I have a form with many text inputs ( <input type="text" /> ). 我有一个带有许多文本输入的表单( <input type="text" /> )。 On pageload, I loop through the form and create a JS object variable for each one, that holds methods such as validate, and properties such as original value, current formatted value, validation requirements, etc: 在页面加载时,我遍历表单并为每个表单创建一个JS对象变量,其中包含诸如validate之类的方法以及诸如原始值,当前格式化值,有效性要求等属性:

field = new Field(wrapperElements[i]);

When an input has the blur event fired, I run validation on that field. 当输入触发了blur事件时,我在该字段上运行验证。 I do this so that I can run my validation as the user is working through the form, rather than wait until the very end when the form is submitted. 我这样做是为了在用户处理表单时可以运行验证,而不必等到提交表单的最后。

I then cache the validity result in the Field object. 然后,将有效性结果缓存在Field对象中。 When the user submits the form, I don't have to validate every field, I just have to check the already calculated validity, which improves speed at the end. 当用户提交表单时,我不必验证每个字段,只需要检查已经计算出的有效性即可,从而提高了速度。


Problem 问题

The user edits only one field, the cursor is still in that field, and the user hits enter. 用户仅编辑一个字段,光标仍在该字段中,然后按Enter。 This fires the form's submit before firing the field's blur event. 这会在触发字段的blur事件之前触发表单的submit The form tries to submit without validating the edited field. 表单尝试提交而不验证已编辑的字段。

I know that I could just force validate all fields on submit , but that seems inelegant to me, and seems like wasted calculation. 我知道,我可能只是强制验证上的所有字段submit ,但似乎不雅给我,好像浪费了计算。 I just need to force validate the currently focused element, if that element is an input. 如果该元素是输入,我只需要强制验证该元素即可。


Part Solution 零件解决方案

I've tested out document.activeElement , and that works. 我已经测试了document.activeElement ,并且可以正常工作。 However, I can't figure out how to get the already existing JS variable that is associated with the input, from the node that document.activeElement returns. 但是,我无法弄清楚如何从document.activeElement返回的节点获取与输入关联的已经存在的JS变量。


Specific Question 具体问题

If I have an HTML element, and I create a JS variable with it as such... 如果我有一个HTML元素,并以此创建一个JS变量...

<input type="text" value="My input!" id="mainInput" />
---
var field = document.getElementById('mainInput');

... how can I get that field variable just from knowing returning the node again, separately, via document.activeElement ? ...如何仅通过知道分别通过document.activeElement返回节点来获取该field变量?

Javascript lets you compare to HTMLElement variables with an == or === comparator. Javascript使您可以使用=====比较器与HTMLElement变量进行比较。 So you could loop through your stored variables, and compare them to document.activeElement . 因此,您可以遍历存储的变量,并将它们与document.activeElement进行比较。

...
fields[i] = form.children[i]
...

for ( ... ) {
    if ( fields[i] == document.activeElement) {
        // force validation of fields[i]
    }
}

This will check the active element against your stored elements. 这将对照存储的元素检查活动元素。 One potential issue here is performance. 这里的一个潜在问题是性能。 If you have a large number of elements, it might take a bit to loop through them. 如果您有大量元素,则可能需要花费一些时间来遍历它们。

Another option if you are super worried about that is during page load, add a data attribute to your element that is an integer, that you can reference the stored variable with. 如果您非常担心这一点,则可以选择另一种选择,那就是在页面加载期间,将一个data属性添加到您的元素中,该属性是一个整数,您可以使用该属性引用存储的变量。

...
fields[i] = form.children[i]
fields[i].dataset.interator = i;
...


var iterator = document.activeElement.dataset.iterator;
// force validation of fields[iterator];

This avoids the loop, and should take just a bit less time. 这样可以避免循环,并且应该花更少的时间。 If you have less than 50 fields though, it shouldn't be noticeable either way. 如果您的字段少于50个,则无论哪种方式都不应该引起注意。

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

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