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

javascript - Can I set an onChange event using CSS? - Stack Overflow

programmeradmin8浏览0评论

I want to have all input elements on my page trigger a function when they change. Can I do it from a CSS?

I tried this:

<STYLE type="text/css">
   input {onChange: function() {alert("foo");};}
</STYLE>

It didn't work in Google Chrome.

I want to have all input elements on my page trigger a function when they change. Can I do it from a CSS?

I tried this:

<STYLE type="text/css">
   input {onChange: function() {alert("foo");};}
</STYLE>

It didn't work in Google Chrome.

Share Improve this question edited Jun 16, 2022 at 12:28 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 14, 2010 at 15:55 EyalEyal 5,8487 gold badges48 silver badges76 bronze badges 2
  • What does CSS have to do with JavaScript and DOM events? – Nikita Rybak Commented Oct 14, 2010 at 15:57
  • to me, thats blasphemy! CSS is for styles my friend, not logic. – Rajat Commented Oct 14, 2010 at 21:09
Add a ment  | 

3 Answers 3

Reset to default 5

CSS doesn't provide eventing. If this is what you want, then I would write a JavaScript function that runs when the page loads. In the function, traverse the form elements for everything that has the tagName input. For each of those, add an event handler.

No you can't that is against the very nature of CSS. The only thing close are Pseudo-Classes like :focus :hover ect, but even they only allow to apply a different Style. So no, you can't just write JS in a CSS File and call it CSS :)

No you can't. About the error in google chrome, use onblur. refer to the following post for more info:
http://www.google./support/forum/p/Chrome/thread?tid=5480bd096a668f1e&hl=en

发布评论

评论列表(0)

  1. 暂无评论