<div id="newsinsite" >gasdgasdgf</div>
<div id="newsinsite" >hasdgfasd</div>
<div id="newsinsite" >gasdgasd</div>
<div id="newsinsite" >gasdgasd</div>
<script>
document.getElementById('style').style.width = "430px";
</script>
Javascript通过newsinsite为所有div提供设置样式。请帮帮我:)。
答案 0 :(得分:1)
这样做:
<div id="newsinsite1" class="newsinsite">gasdgasdgf</div>
<div id="newsinsite2" class="newsinsite">gasdgasdgf</div>
<div id="newsinsite3" class="newsinsite">gasdgasdgf</div>
<div id="newsinsite4" class="newsinsite">gasdgasdgf</div>
<script>
var elem = document.getElementsByClassName("newsinsite");
for (var i=0;i<elem.length;i++) {
elem[i].style.width = "430px";
}
</script>
但如果这是静态设置,请改用css:
<style>
.newsinsite {
width: "430px";
}
</style>
更新
对于IE8支持,请使用querySelectorAll('.classname')
代替getElementsByClassName
答案 1 :(得分:0)
<div class="newsinsite">gasdgasdgf</div>
<div class="newsinsite">hasdgfasd</div>
<div class="newsinsite">gasdgasd</div>
<div class="newsinsite">gasdgasd</div>
<script>
var i = 0, els = document.getElementsByClassName('newsinsite'), len = els.length;
for(;i<len;i++) {
els[i].style.width = '430px';
}
</script>
或者,用CSS设置它:
.newsinsite {
width: 430px;
}