,然后使用
require([‘dijit/form/Button’], function(Button){ / 使用Button / });`来加载和使用Dijit组件。
Dijit 是 Dojo 工具包的富组件用户界面库,提供了一系列丰富的用户界面组件,如日历、调色板、对话框等,这些组件可以完全主题化,并可以通过 HTML 标签或 JavaScript 编程方式进行声明和使用。
要使用 Dijit,首先需要引入 Dojo 核心库和所需的主题样式表,以下是一个简单的示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Dijit 示例</title> <!-引入 Dojo 核心库 --> <script src="https://ajax.googleapis.com/ajax/libs/dojo/1.17.4/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script> <!-引入Claro主题样式表 --> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/dojo/1.17.4/dijit/themes/claro/claro.css"> </head> <body class="claro"> <!-页面内容 --> </body> </html>
1. 声明式创建 Dijit 组件
通过在 HTML 标签中添加dojoType
属性来声明 Dijit 组件,创建一个按钮:
<button id="myButton" dojoType="dijit.form.Button" label="点击我"></button>
2. 编程式创建 Dijit 组件
通过 JavaScript 代码动态创建 Dijit 组件,创建一个下拉框:
require(["dijit/form/FilteringSelect", "dojo/domReady!"], function(FilteringSelect){ var select = new FilteringSelect({ name: "players", options: [ {label: "Andrey Arshavin", value: "Arshavin"}, {label: "Thomas Vermaelen", value: "Vermaelen"} ] }, "players"); select.startup(); });
Dijit 提供了多个内置主题,如 Claro、Tundra、Soria 和 Nihilo,可以通过在<body>
标签上设置相应的类名来应用主题,或者通过 CSS 导入相应的主题样式表。
Q1: 如何在网页中使用 Dijit 组件?
A1: 在网页中使用 Dijit 组件,首先需要引入 Dojo 核心库和所需的主题样式表,可以通过在 HTML 标签中添加dojoType
属性来声明式创建 Dijit 组件,或者通过 JavaScript 代码编程式创建 Dijit 组件。
Q2: Dijit 提供了哪些内置主题?如何应用这些主题?
A2: Dijit 提供了 Claro、Tundra、Soria 和 Nihilo 四个内置主题,可以通过在<body>
标签上设置相应的类名(如class="claro"
)来应用主题,或者通过 CSS 导入相应的主题样式表(如@import "https://ajax.googleapis.com/ajax/libs/dojo/1.17.4/dijit/themes/claro/claro.css";
)来应用主题。