首页 > 解决方案 > 如何使用 javascript 更改 figcaption 的内容?

问题描述

    <button onclick="changefigure()">2011</button>
    <figure>
        <img id="Image" src="NT_Naplan_Reading_Results_2017.png"/>
        <figcaption id="caption"><em>Fig 2</em>Percent of children above national minimum standard in 
        reading in 2017 for Year 3, 5, 7 and 9 for Non-Indigenous and Indigenous children in the 
        Northern Territory. Data Source <a href="">NAPLAN results</a></figcaption>
    </figure>

    <script>
        function changefigure()
        {
            var x = document.getElementById("Image");
            var y = document.getElementById("caption")
            x.src = 'NT_Naplan_Reading_Results_2011.png'
            //y = "<em>Fig 1</em>Percent of children above national minimum standard in reading in 
            2017 for Year 3, 5, 7 and 9 for Non-Indigenous and Indigenous children in the Northern 
            Territory. Data Source <a href="">NAPLAN results</a>"
        }
    </script>

所以我似乎无法改变figcaption。我试过使用 y.innerHTML 和 y.innertext 但它们似乎都不起作用。有小费吗?

标签: javascripthtml

解决方案


所以我发现我的代码有什么问题。而不是使用“使用”来代替它。EG

<script>
    function changefigure()
    {
        var x = document.getElementById("Image");
        var y = document.getElementById("caption")
        x.src = 'NT_Naplan_Reading_Results_2011.png'
        y.innerHTML = '<em>Fig 1</em>Percent of children above national minimum 
        standard in reading in 2017 for Year 3, 5, 7 and 9 for Non-Indigenous and 
        Indigenous children in the Northern Territory. Data Source <a href="">NAPLAN 
        results</a>'
    }
</script>

推荐阅读