Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  6] [ 1]  / answers: 1 / hits: 56611  / 13 Years ago, mon, november 28, 2011, 12:00:00

Is it possible in my css file to do something like that?:



.myclass:after{
content:click me;
onclick:my_function();
}


I want to add after all instances of myclass a clickable text, in the css style sheet.


More From » html

 Answers
73

Is it possible in my css file to do something like [see code above]?




No



The important question to ask is why.



HTML has control of the data within the webpage. Any CSS or JS is specified via the HTML. It's the Model.



CSS has control of the styles, there is no link between CSS and HTML or JavaScript. It's the View.



JavaScript has control of the interactions within the webpage, and has hooks to any and all DOM nodes. It's the Controller.



Because of this MVC structure: HTML belongs in .html files, CSS belongs in .css files, and JS belongs in .js files.



CSS pseudo-elements do not create DOM nodes. There is no direct way for JavaScript to access a pseudo-element defined in CSS, and there's no way to attach an event to said pseudo-elements.



If you've got a set structure in place, and can't add the additional content necessary to produce new links within the HTML, JavaScript can dynamically add the new elements necessary which can then be styled via CSS.



jQuery makes this very simple:



$('<span class=click-me>click me</span>').appendTo('.myclass').click(my_function);

[#88865] Saturday, November 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravenl

Total Points: 338
Total Questions: 107
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
ravenl questions
Thu, Feb 18, 21, 00:00, 3 Years ago
Tue, Jan 12, 21, 00:00, 3 Years ago
Tue, Mar 17, 20, 00:00, 4 Years ago
;