在for循环中附加的javascript不起作用,但alering不起作用
我试图在for循环中附加到字符串。我可以提醒每个循环的值,但由于某些原因,我无法追加:在for循环中附加的javascript不起作用,但alering不起作用,javascript,loops,Javascript,Loops,我试图在for循环中附加到字符串。我可以提醒每个循环的值,但由于某些原因,我无法追加: <html> <head> <script type="text/javascript" language="javascript"> function doIt(){ var styleSheet = document.styleSheets[0];
<html>
<head>
<script type="text/javascript" language="javascript">
function doIt(){
var styleSheet = document.styleSheets[0];
var css="";
for (i=0; i<=styleSheet.cssRules.length; i++)
{
css += styleSheet.cssRules[i].cssText;
//alert(styleSheet.cssRules[i].cssText); //WORKS
}
}
</script>
<style type="text/css">
.container{display:none;}
.markup-container{color:#404040;}
.title{text:decoration:underline;}
.body{color:#000;}
</style>
</head>
<body>
<input type="button" id="button" onmousedown="doIt()">
<div class="container">
<div class="markup-container">
<div class="title">This is a title with some markup</div>
<div class="body">This is the body with some markup and it's longer ^^</div>
</div>
</div>
</body>
</html>
函数doIt(){
var styleSheet=document.styleSheets[0];
var css=“”;
for(i=0;i您的for
循环关闭了一次:)
您将得到一个错误,因为您超过了数组的长度,并且样式表.cssRules[i]
是未定义的
,当您尝试访问.cssText
属性时,会导致一个错误。当您说“不能追加”时,这意味着什么?字符串仍然是空的?您可能需要尝试调用toString()在cssText上,虽然您不必这样做。我可以做一些类似于css+=“somestring”
的事情。
for (i=0; i<=styleSheet.cssRules.length; i++)
//should be:
for (i=0; i<styleSheet.cssRules.length; i++)
styleSheet.cssRules[styleSheet.cssRules.length].cssText