最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using .onKeyUp and .onChange with Javascript DOM objects - Stack Overflow

programmeradmin2浏览0评论

I'm having a weird problem with some JavaScript/DOM code I've been playing with. I'm trying to assign the .onKeyUp and .onChange events/methods to a text input like so:

form.elements["article"].onkeyup = "alert('test');";

Oddly, assigning using that method is doing nothing, and I'm forced to do this manually using:

form.elements["article"].setAttribute("onkeyup", "alert('test');");

Am I missing something here? I've used the first method I mentioned before and it has worked fine.

I'm having a weird problem with some JavaScript/DOM code I've been playing with. I'm trying to assign the .onKeyUp and .onChange events/methods to a text input like so:

form.elements["article"].onkeyup = "alert('test');";

Oddly, assigning using that method is doing nothing, and I'm forced to do this manually using:

form.elements["article"].setAttribute("onkeyup", "alert('test');");

Am I missing something here? I've used the first method I mentioned before and it has worked fine.

Share Improve this question edited Jul 27, 2022 at 16:56 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 25, 2009 at 5:27 bloudermilkbloudermilk 18.1k19 gold badges72 silver badges99 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Try this:

form.elements["article"].onkeyup = function() { alert("test"); };

Steve

You need to assign a function, not a string. For example:

form.elements["article"].onkeyup = function(){alert('test');};

The only things I know that will take a string and eval it (other than eval) are setTimeout and setInterval.

发布评论

评论列表(0)

  1. 暂无评论