没有Jquery的Dom事件TextArea更改 [英] Dom Event TextArea Change without Jquery

查看:87
本文介绍了没有Jquery的Dom事件TextArea更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个项目并且为了更快,我选择不使用jQuery(我只使用库的全部潜力的5%)。

I'm creating a project and for it to be faster, I have chosen not to use jQuery(I would use only 5% of the full potential of the library).

在这个项目中,我有一个< textarea> 元素,每次更改时都需要获取内容。我尝试了不同的例子,但都没有。

In this project, I have a <textarea> element, and need to get the contents every time it changes. I have tried different examples, but none worked.

如何使用Vanilla JavaScript和原生DOM事件编写以下代码?

How do I write the following code using Vanilla JavaScript and native DOM Events?

$("#textarea").bind('input propertychange')
// or
$("#textarea").bind('change')


推荐答案

jQuery .change()是native 更改事件。

jQuery .change() is an alias for native change event.


针对<触发更改事件code>< input> ,< select> < textarea> 用户提交对元素值的更改时,$ c>
元素。与输入事件不同,对于元素值的每次更改,更改事件不一定是

The change event is fired for <input>, <select>, and <textarea> elements when a change to the element's value is committed by the user. Unlike the input event, the change event is not necessarily fired for each change to an element's value.

您可以使用它非常简单:

You can use it fairly simple:

// Non-obtrusive JavaScript example(preffered).    
element.addEventListener('change', callback, false);

// Somewhat obtrusive (not recommended).
element.onchange = function () { ... };

// Obtrusive JavaScript in HTML (not recommended).
<input type="text" onchange="function() { ... };">

这篇关于没有Jquery的Dom事件TextArea更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆