Embedding JavaScript in CSS
While it's not directly possible to execute JavaScript within CSS, there are creative techniques that allow for similar functionality.
IE: Expression Technique and HTC Behavior
Internet Explorer provides two methods:
body { color: (function() { return document.getElementById("button").style.color; })(); }
body { behavior:url(script.htc); }
<PUBLIC:COMPONENT TAGNAME="xss"> <PUBLIC:ATTACH EVENT="ondocumentready" ONEVENT="main()" LITERALCONTENT="false"/> </PUBLIC:COMPONENT> <SCRIPT> function main() { alert("HTC script executed."); } </SCRIPT>
Firefox: XBL
Firefox offers a similar XML-based solution called XBL.
body { -moz-binding: url(script.xml#mycode); }
<bindings xmlns="http://www.mozilla.org/xbl" xmlns:html="http://www.w3.org/1999/xhtml"> <binding>
Important Considerations
The above is the detailed content of How Can I Embed JavaScript Functionality in CSS?. For more information, please follow other related articles on the PHP Chinese website!