search

Home  >  Q&A  >  body text

javascript - Why can't my code set attributes on elements?

    <h1 class="MyClass">一</h1>
    <script>
        var hs =document.getElementsByTagName('h1');
        alert(hs[0].className);
        hs[0].className='XXXX';
    </script>

This is this code. Why can't I set a new value for this tag? setAttribute() doesn't work either.

習慣沉默習慣沉默2749 days ago511

reply all(3)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:47:09

    Is there any interference from other h1 tags? I can use your code to test it and it’s fine

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:47:09

    Are these the only codes? It should be fine. . .

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-19 10:47:09

    Can be set.

    reply
    0
  • Cancelreply