Originariamente inviato da cavicchiandrea
Se intendi con js modificare il css no non puoi, devi modificare il div (o oggetto del documento) sempre tramite js.
Non affrettiamoci a dare delle risposte se non siamo assolutamente certi. Con javascript puoi modificare direttamente un foglio di stile, senza agire sul singolo elemento... come in quest'esempio, che potrebbe fare al caso tuo:
codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Esempio</title>
<script type="text/javascript">
function changeStyleSheet(sSelector, sProperty, sValue) {
var bIsNewProp = true, aQueryList = [], rQuery = new RegExp(sSelector + "(?!\\w)");
for (var iStyleSheetId = 0, nSelectorsLen = document.styleSheets.length; iStyleSheetId < nSelectorsLen; iStyleSheetId++) {
if (document.styleSheets[iStyleSheetId].cssRules) { oRules = document.styleSheets[iStyleSheetId].cssRules; }
else if (document.styleSheets[0].rules) { oRules = document.styleSheets[0].rules; }
else { return; }
for (iThisRule in oRules) {
if (rQuery.test(oRules[iThisRule].selectorText)) { aQueryList.push(oRules[iThisRule]); }
}
}
var nQueryLen = aQueryList.length;
if (nQueryLen === 0) { return; }
for (var iQueryId = 0; iQueryId < nQueryLen; iQueryId++) {
if (aQueryList[iQueryId].style[sProperty]) { aQueryList[iQueryId].style[sProperty] = sValue; bIsNewProp = false; break; }
}
if (bIsNewProp) { aQueryList[0].style[sProperty] = sValue; }
}
</script>
<style type="text/css">
#elementoACaso {
text-decoration: underline;
color: #ff00ff;
}
</style>
<style type="text/css">
.intLink {
cursor: pointer;
text-decoration: underline;
color: #0000ff;
}
#container {
width: 100px;
height: 50px;
background-color: #ff0000;
}
.intLink2 {
cursor: pointer;
text-decoration: underline;
color: #0000ff;
}
</style>
</head>
<body>
<div id="container">Il tuo div</div>
<span class="intLink" onclick="changeStyleSheet('#container', 'width', '400px');">Clicca qui</span></p>
</body>
</html>