如何在 Javascript 中将事件连接到 HTML 元素?

发布于 2024-11-07 01:54:52 字数 263 浏览 10 评论 0原文

我很难找到跨浏览器兼容的、基于 Javascript 的方法来将函数附加到元素。看来微软(当然)再次有自己的做事方式,并且让这样做变得困难。基本上,我在 Javascript 中动态创建一个表,并将 TEXTAREA 元素添加到行中的单元格中。我需要向 TEXTAREA 的 onkeydown 添加一个函数,以便检查文本框的长度是否达到最大限制(不幸的是,TEXTAREA 元素不支持 MaxLength 属性)。

是否存在适用于 FF 和 IE6、IE7、IE8 和 IE9 的简单解决方案?

I am having difficulty finding a cross-browser-compatible, Javascript-based method to attach a function to an element. It seems that Microsoft (of course) has its own way of doing things yet once again and is making it difficult to do this. Basically, I am creating a table dynamically in Javascript and am adding TEXTAREA elements to cells in the row. I need to add a function to the onkeydown for the TEXTAREA so I can check whether or not the length of the textbox has reach a max limit (TEXTAREA elements don't support a MaxLength property unfortunately).

Does a simple solution to this exist that works for FF and IE6, IE7, IE8, and IE9?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音盲 2024-11-14 01:54:52

我建议您看一下 jQuery。一个例子是

$('textarea').keydown(function () { });

注意,在这个例子中,我将事件处理程序附加到每个文本区域。更好的方法是使用 css 类或其他更具体的选择来找到正确的文本区域。例如:

$('textarea.limitedlength').keydown(function () { });

另请注意,您应该在创建元素后执行此操作。如果您不想为计时而烦恼,请使用 live 函数,然后 jQuery 会将事件处理程序附加到任何新找到的元素。

$('textarea.limitedlength').live('keydown', function () { });

好处是任何跨浏览器问题都由框架处理。 (我不知道 keydown 事件有任何跨浏览器问题,但 jQuery 处理设置事件处理程序和选择正确元素方面的差异)。

正如 RobG 在他的回答中指出的那样,由于粘贴, keydown (和 keyup 事件)是不够的。另一种方法是计算并检查焦点何时从文本区域移除:

$('textarea.limitedlength').blur(function () { });

I would suggest that you take a look at jQuery. An example would then be

$('textarea').keydown(function () { });

Note that in this example I attach the eventhandler to every textarea. A better way would be to use css classes or another more specific selection to find the right textareas. For example:

$('textarea.limitedlength').keydown(function () { });

Also note that you should do this after creating the element. If you do not want to bother with timing, use the live function, jQuery will then attach the eventhandler to any new found element.

$('textarea.limitedlength').live('keydown', function () { });

The benefit is that any cross-browser problems are handled by the framework. (I'm not aware of any cross-browser problems with the keydown event, but jQuery handles the differences in setting up event handlers and selecting the right elements).

As RobG notes in his answer, the keydown (and keyup events) aren't enough, because of pasting. Another approach would be to count and check when the focus is removed from the textarea:

$('textarea.limitedlength').blur(function () { });
江湖正好 2024-11-14 01:54:52

使用按键事件检查文本区域内容的问题在于,还有其他输入文本的方式,例如粘贴或拖动。许多人使用 setIntervalsetTimeout 来检查内容并不时更新计数(例如 50 毫秒),但此类方法的计算效率可能较低。仅当文本区域具有焦点时,您才可以运行计时器。

还有 HTML5 input 事件类型,每当表单控件有输入时就会触发:

<textarea oninput="document.getElementById('msg').innerHTML = this.value.length;">
</textarea>
<div id="msg"></div>

但当然并非所有浏览器都支持该类型。

The problem with using key events to check on textarea content is that there are other ways of entering text, such as paste or drag. Many use setInterval or setTimeout to check on the content and update the count from time to time (say 50ms), but such approaches can be computationally inefficient. You might be able to have the timer running only when the textarea has focus.

There is also the HTML5 input event type that fires whenever a form control has input:

<textarea oninput="document.getElementById('msg').innerHTML = this.value.length;">
</textarea>
<div id="msg"></div>

But of course not all browsers support that.

分分钟 2024-11-14 01:54:52

除非您需要附加多个事件侦听器,否则这种方法可以正常工作:

element.onkeydown = function(e)
{
    e = e || window.event;
    ...
}

Unless you need to attach more than one event listeners, this works fine:

element.onkeydown = function(e)
{
    e = e || window.event;
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文