|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Moifying css styles in html file with js is easy enough, but what external css with external js? I can't figure out how to reference the external style sheet with my js. Any one have an example piece of code? Thanks.
Sean |
|
#2
|
|||
|
|||
|
Basically, it does not make a difference whether you're dealing with an external stylesheet or inside the document.
Suppose, you have a stylesheet 'mystyle.css': Code:
.myclass {
font-family: verdana, sans-serif;
font-size: 2em;
background-color: red;
}
You could manipulate it by using the following JavaScript code: Code:
<script type="text/javascript">
function changecss(myclass,element,value) {
var CSSRules
if (document.all) {
CSSRules = 'rules'
}
else if (document.getElementById) {
CSSRules = 'cssRules'
}
for (var i = 0; i < document.styleSheets.length; i++) {
for (var j = 0; j < document.styleSheets[i][CSSRules].length; j++) {
if (document.styleSheets[i][CSSRules][j].selectorText == myclass) {
document.styleSheets[i][CSSRules][j].style[element] = value
}
}
}
}
and call this function with: Code:
<div onclick="changecss('.myclass','backgroundColor','blue')">changecss</div>
<div class="myclass">
myclass content
</div>
Hope this helps, Jeroen |
|
#3
|
||||
|
||||
|
i believe what would be even ezr would be to do this:
Code:
function changeCSS(cssFile)
{
cssLink.href=cssFile;
}
and then give your <link> tag an id of cssLink and when you call it, call it like so: onCLick="changeCSS('path_to_your_css_file.css')" http://terminal.ledohost.com/layout1.html is an example of mine |
![]() |
| Viewing: Dev Shed Forums > Web Design > CSS Help > External JS & CSS |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|