### [小程序怎么用js修改css](https://www.prosaas.cn/article/16488) **Published:** 2020-12-21T02:29:53 **Author:** 5201314 **Excerpt:** 小程序用js修改css的方法:首先页面动态绑定样式变量;然后data对象中初始化变量;接着生命周期函数变量赋值… > 小程序用js修改css的方法:首先页面动态绑定样式变量;然后data对象中初始化变量;接着生命周期函数变量赋值;最后保存代码左侧查看效果。 ![](https://img.php.cn/upload/article/000/000/052/5fe0077e65e91816.jpg) 本文操作环境:Windows7系统,微信小程序开发工具2019版本,Dell G3电脑。 **相关免费推荐:****小程序开发教程** **小程序用js修改css的方法:** 1、打开微信小程序开发工具,新建一个wxml文件,并添加view和button,设置样式并绑定字体颜色变量 ![](https://img.php.cn/upload/image/586/349/196/1608517579252109.png) 2、打开对应页面的js文件,在data对象中初始化变量color ![](https://img.php.cn/upload/image/359/238/523/1608517588456336.png) 3、在生命周期函数onLoad中,使用`this.setData({color:'#000'})` ![](https://img.php.cn/upload/image/360/448/920/1608517596971053.png) 4、保存代码并刷新,查看左侧模拟器对应页面模块,看到按钮字体颜色为黑色 ![](https://img.php.cn/upload/image/139/920/310/1608517603707495.png) 5、再在页面代码style中,添加font-size并绑定变量fontSize ![](https://img.php.cn/upload/image/622/852/439/1608517610460892.png) 6、再在data对象中初始化变量fontSize,然后在onLoad中给fontSize赋值 ![](https://img.php.cn/upload/image/725/577/165/1608517615804805.png) 7、再次保存代码并查看左侧模拟器,可以查看到字体变大了 ![](https://img.php.cn/upload/image/363/552/951/1608517620196042.png) 以上就是小程序怎么用js修改css的详细内容,更多请关注知企PROSAAS其它相关文章! **Categories:** 后端开发 ---