问题描述
我看到突变观察者曾经使用过诸如google chrome开发人员工具那样修改了doms的属性.但是,当 textarea
中的文本由于用户键入或粘贴而发生更改时,我找不到如何调用函数的方法.在我的代码中,即使所有观察选项都设置为true,当用户键入回调时也不会被调用.这是什么代码?
I have seen mutation observers used to obtain the properties of doms when they are modified such as with the google chrome developer tools. I can't, however, find how to call a function when the text within a textarea
changes due to a user typing or pasting. In my code, as the user types the callbacks don't get called, even with all the observe options set to true. What is the code for this?
推荐答案
变异观察者侦听DOM的更改.但是,表单元素的当前状态不能由DOM反映出来.
The mutation observers listen for changes of the DOM. The current state of form elements, however, is not reflected by the DOM.
例如,创建输入元素 input 而不设置value属性.在输入字段中输入文本时, input.value 会反映该文本.另一方面, input.getAttribute('value')仍返回 null .
For example, create an input element input without setting the value attribute. When text is being entered into the input field, input.value reflects this text. On the other hand, input.getAttribute('value') still returns null.
因此,变异观察者无法观察到表单字段状态的这种变化.
Therefore, the mutation observers can't observe this change in the form field's status.
(提议的ECMAScript 6th Edition的Object.observe也无济于事.尽管可以在最新版的Chrome中侦听宿主对象的更改,但宿主对象属性的更改(例如 input 未被观察到.)
(Object.observe of the proposed ECMAScript 6th edition also doesn't help. Although one can listen on changes of host objects in recent versions of Chrome, changes of host object properties like value of input above are not being observed.)
这篇关于您如何获得一个变异观察者来检测文本区域的值变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!