简体   繁体   English

如何更改 html img 元素源女巫飞镖?

[英]How can I change the html img element source witch dart?

I have this img element in my HTML project:我的 HTML 项目中有这个 img 元素:

<img id="themeToggle" src="./images/moon.svg">

and i want to change the source of this element to be "./images/sun.svg".我想将此元素的来源更改为“./images/sun.svg”。

I tried with:我试过:

void main() {
    var themeToggleButton = querySelector('#themeToggle');
    themeToggleButton?.onClick.listen((event) {
        themeToggleButton.dataset['src'] = './images/sun.svg';
    });
}

since the .dataset attribute is the only one that lets you to access the selected element's attributes, but it does not work.因为 .dataset 属性是唯一可以让您访问所选元素属性的属性,但它不起作用。 Any suggestion, please?请问有什么建议吗?

You should be able to use the .attributes getter to get the attributes Map and set there the value:您应该能够使用.attributes getter来获取属性 Map 并在那里设置值:

themeToggleButton?.attributes['src'] = './images/sun.svg'

Note: .dataset (as specified in the docs) is used only for the element properties that start with data-注意: .dataset (在文档中指定)仅用于以data-开头的元素属性 -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM