类内的值不变

时间:2015-02-21 07:27:00

标签: javascript html css

我想改变类中的值,但它没有改变 我的代码 // HTML

<menu class="box">a</menu><menu class="box">b</menu><menu class="box">c</menu><menu class="box">d</menu>

// java描述

<script type="text/javascript">

var box = document.getElementsByClassName('box');
var after = ['html', 'css', 'js' , 'jquery']

for (i = 0; i < box.length; i++) {
  box[i].value = after[i];

}

2 个答案:

答案 0 :(得分:1)

要更改班级名称,请使用

var boxes = document.getElementsByClassName('box');
var after = ['html', 'css', 'js' , 'jquery']

for (i = 0; i < box.length; i++) {
  boxes[i].className = after[i];
}

来自MDN Docs

哪会给你

<menu class="html">a</menu><menu class="css">b</menu>
<menu class="js">c</menu><menu class="jquery">d</menu>

要更改每个元素的实际值,您可以使用innerHtml

var boxes = document.getElementsByClassName('box');
var after = ['html', 'css', 'js' , 'jquery']

for (i = 0; i < box.length; i++) {
  boxes[i].innerHTML = after[i];
}

哪会给你

<menu class="box">html</menu><menu class="box">css</menu>
<menu class="box">js</menu><menu class="box">jquery</menu>

答案 1 :(得分:0)

&#13;
&#13;
var box = document.getElementsByClassName('box');
var after = ['html', 'css', 'js', 'jquery'];
var j = 2;
for (i = 0; i < box.length; i++) {

  $('.box:nth-child(' + j + ')').html(after[i]);
  var j = j + 1;

}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<menu class="box">a</menu>
<menu class="box">b</menu>
<menu class="box">c</menu>
<menu class="box">d</menu>
&#13;
&#13;
&#13;

试试他的