2015-05-29 50 views
4

我正在研究一个考试项目,我需要在页面中更改样式表的名称。我如何用Dart做到这一点? 我已经看到,我可以从Dart编辑我的css的每个属性,但是我已经在其他页面中使用了其他CSS。 例如,我有这个在我的html页面:如何使用dart更改css样式表?

<link href="stile.css" rel="stylesheet" type="text/css"> 

我需要编辑的页面,使这个

<link href="stile-blu.css" rel="stylesheet" type="text/css"> 

我怎样才能做到这一点?

+0

你在哪儿这个链接元素?在标题中,印在身上?在StackOverflow中,最好添加一些代码,这些代码显示了你已经尝试了什么,你得到了什么错误信息或者你遇到了什么其他问题。 –

回答

3

假设你<link ...>元素在<head>元素

document.querySelector('#button').onClick.listen((_) { 
    var stile = document.head.querySelector('link[href="stile.css"]'); 
    stile.attributes['href'] = 'stile-blu.css'; 
}); 

如果添加id属性您的链接元素的选择可以简化

<link id="mystyle" href="stile.css" rel="stylesheet" type="text/css"> 
document.querySelector('#button').onClick.listen((_) { 
    var stile = document.head.querySelector('#mystyle'); 
    stile.attributes['href'] = 'stile-blu.css'; 
});