setProperty(id, property, value) 设置控件属性

功能分类: UI Controls Applab JavaScript 少儿编程

设置指定 id 控件指定的 property 属性的值。

一般情况下,你在 Applab 里的 设计 模式下可以更容易更直观地设置所有页面控件的属性。但有时候你也需要在 APP 应用运行时根据用户操作及触发的不同事件,相应地动态修改一些控件的属性。这时候就可以使用 setProperty() 方法来做到这一点。使用这个方法,你可以对控件在 设计 模式下显示的所有属性进行修改。

示例代码



// 修改按钮颜色及字体大小
button("myButton", "Click me!");
setProperty("myButton", "background-color", "red");
setProperty("myButton", "font-size", 20);

语法规则


setProperty(id, property, value);

参数说明

名称 类型 必需 参数描述
id string Yes 指定控件的 id 唯一标识。必须以字母开头,不能包含空格,可以包含字母、数字、减号 - 以及下划线 _ 。
property string Yes 指定要设置的属性名称。
value string, number, or boolean Yes 指定要设置属性的值。属性值的类型取决于指定的属性名称。不同属性对应值的类型可能是不一样的。

可能的属性

属性名称 属性值 对哪些控件有效
"width" number 设计 模式下除 screen 外所有类型的控件
"height" number 设计 模式下除 screen 外所有类型的控件
"x" number 设计 模式下除 screen 外所有类型的控件
"y" number 设计 模式下除 screen 外所有类型的控件
"text-color" color string Button、text input、label、dropdown 以及 text area
"background-color" color string Button、text input、label、dropdown、screen 以及 text area
"font-size" number Button、text input、label、dropdown 以及 text area
"text-align" string ("left", "right", "center", or "justify") Button、label 以及 text area
"hidden" boolean 设计 模式下除 screen 外所有类型的控件
"text" string Button、label 以及 text area
"placeholder" string Text input
"image" string Button、image
"group-id" string Radio button
"checked" boolean Radio button、checkbox
"readonly" boolean Text area
"options" list of values Dropdown
"value" number Slider
"min" number Slider
"max" number Slider
"step" number Slider

返回值

没有返回值。只在显示结果页面体现代码执行前后变化。

提示说明

  • 设计 模式里的相关代码模块里,相关属性选择的下拉列表将自动筛选出当前控件相关的属性。与当前控件不相关的属性不会出现在列表里。
  • 相关方法:可以使用 getProperty() 方法来获取控件属性的值。

查看更多少儿编程教程、JavaScript 介绍

返回文档首页