【问题标题】:How do I change the content of figcaption using javascript? [duplicate]如何使用 javascript 更改 figcaption 的内容? [复制]
【发布时间】:2020-12-21 04:20:27
【问题描述】:
    <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 但它们似乎都不起作用。有什么建议吗?

【问题讨论】:

  • 您遇到的具体问题是什么?您是否在控制台中收到任何错误?请向我们展示您用于在 JS 中设置 figcaption 的代码,以便我们查看可能导致问题的原因。
  • 它应该可以工作。可能是您有语法错误,因为您以错误的方式构建新字符串。将多行字符串放在两个 ` 字符内。

标签: javascript html


【解决方案1】:

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

<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>

【讨论】:

  • 在 Javascript ES6 中,如果您有多行字符串或字符串插值,建议使用 template literals
猜你喜欢
  • 2023-03-28
  • 2018-04-05
  • 2010-12-11
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
相关资源
最近更新 更多