我试图实现相当于用纯JavaScript编写的jQuery(下面):
$('input[name=\"myelementname\"]').keyup();
Run Code Online (Sandbox Code Playgroud)
到目前为止,我还没有找到一个简单的解决方案.有任何想法吗?
首先,您必须创建一个新事件:
let keyupEvent = new Event('keyup');
Run Code Online (Sandbox Code Playgroud)
然后使用EventTarget.dispatchEvent():
// here we use Array.from() to convert the NodeList
// returned from the document.getElementsByNames...
// call into an Array:
Array.from(
// document.getElementsByName() retrieves all the
// elements on the page with the supplied name:
document.getElementsByName('myelementname')
// we iterate over that Array using Array.prototype.forEach():
).forEach(
// here we use an Arrow function, the 'input' is a reference
// to the current element (regardless of element-type, since
// we selected by name) of the Array-element of the Array
// over which we're iterating.
// we trigger the named event (using a cached copy of that
// created Event) on the element:
input => input.dispatchEvent(keyupEvent)
);
Run Code Online (Sandbox Code Playgroud)
参考文献:
参考书目:
| 归档时间: |
|
| 查看次数: |
3698 次 |
| 最近记录: |