removeAttribute导致未定义

这是我编写并停留的前几行Javascript。

    <div id=form_template2 style="border: 1px solid black"></div>

    <script>
        const form_template = document.getElementById('form_template2')
        console.log(form_template.removeAttribute("style"))
    </script>

一旦添加.removeAttribute('style')位,我就在console.log中变得未定义:/

评论
  • inisi
    inisi 回复

    Some functions in JavaScript just don't return anything. And in JavaScript world, that nothing is undefined. When you define your own function as:

    function lazy() {
        // does nothing
    }
    

    And then you try console.log(lazy()) on it, the result will be the same - undefined.

  • fharum
    fharum 回复

    removeAttribute doesn't return a value, so you'll get undefined. Check out the documentation of the function at https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttribute