如何在HTML中触发onChange事件,即使在js中内容发生了变化?

发布于 2024-12-10 08:32:53 字数 260 浏览 2 评论 0原文

我知道 HTML onChange 事件仅在元素模糊时才会触发。

但是有没有办法在文本框的内容发生变化时运行脚本呢?即使在Javascript中改变了?

我的表单中有一个文本框来选择日期。我还有一个带有日历的按钮来选择日期。该日历写在文本框中,我想在日期更改时执行一些操作。

我无法更改日历的 Javascript。

我还希望与所有主要浏览器兼容(IE7-9、Firefox、Chrome、Safari、Opera...)。

I know the HTML onChange event is triggered only when the element is blurred.

But is there a way to run a script when the content of a textbox changes? Even if it is changed in Javascript?

I have a textbox in my form to select a date. I also have a button with a calendar to pick a date. This calendar writes in the textbox and I want to do something when the date changes.

I cannot change the Javascript of the calendar.

I also want compatibility with all major browsers (IE7-9, Firefox, Chrome, Safari, Opera, ...).

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

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

发布评论

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

评论(2

梦情居士 2024-12-17 08:32:53

您正在寻找突变事件(例如DomAttrModified)。

请参阅使用 jQuery 检测元素内容更改(如果这对初学者有帮助)。

You're looking for mutation events (eg. DomAttrModified).

See Detect element content changes with jQuery if that would help you as a starter.

风铃鹿 2024-12-17 08:32:53

我找到了我的问题的答案,可能不是更好的答案,但仍然有效。它使用 JavaScript 定时器。

function watchElement()
{
    var newVal = document.FormName.TextboxName.value;
    if(oldVal != newVal) {FunctionToCall();}
    oldVal = newVal;
}

var oldVal = document.FormName.TextboxName.value;
setInterval(watchElement, 500);

我知道这不是世界上更好的代码,但它适用于所有主要浏览器。

I found an answer to my question wich is maybe not the better one, but still works. It uses javascript timers.

function watchElement()
{
    var newVal = document.FormName.TextboxName.value;
    if(oldVal != newVal) {FunctionToCall();}
    oldVal = newVal;
}

var oldVal = document.FormName.TextboxName.value;
setInterval(watchElement, 500);

I know this is not the better code in the world, but it works for every major browsers.

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