|
| 1 | +# Getting Start with Dom |
| 2 | + |
| 3 | +With Fastjs's dom module, you can operate dom elements with simple codes. |
| 4 | + |
| 5 | +Some API of Fastjs's dom module looks like jQuery, but not fully the same way. |
| 6 | + |
| 7 | +Fastjs redesigned a lot of API to help you write in different ways, and we provide strongly type support without any extra packages. |
| 8 | + |
| 9 | +## Import Module |
| 10 | + |
| 11 | +```typescript |
| 12 | +import { dom } from "jsfast" |
| 13 | +``` |
| 14 | + |
| 15 | +## Select a element |
| 16 | + |
| 17 | +:::tip Different with jQuery |
| 18 | +Fastjs doesn't register a global variable `$` to select elements, import `dom` and use `dom.select()` to select elements. |
| 19 | +::: |
| 20 | + |
| 21 | +Select a element to operate is usually the things you need to do first, you can do this easliy with `dom.select()` |
| 22 | + |
| 23 | +```typescript |
| 24 | +dom.select("#id"); // FastjsDom | null |
| 25 | +dom.select(".class"); // FastjsDomList | null |
| 26 | +dom.select("tag"); // FastjsDomList | null |
| 27 | +``` |
| 28 | + |
| 29 | +With strongly type support with your typescript project, you can define the return type of `dom.select()`. |
| 30 | + |
| 31 | +```typescript |
| 32 | +import type { FastjsDom, FastjsDomList } from "jsfast"; |
| 33 | + |
| 34 | +const idElement = dom.select<FastjsDom>("#id"); |
| 35 | +const classElement = dom.select<FastjsDomList>(".class"); |
| 36 | +const tagElement = dom.select<FastjsDomList>("tag"); |
| 37 | +``` |
| 38 | + |
| 39 | +## Operate a element |
| 40 | + |
| 41 | +After you select a element, you can operate it with some [methods](./api.md). |
| 42 | + |
| 43 | +```typescript |
| 44 | +dom.select("h1")!.html("Hello World"); |
| 45 | +``` |
| 46 | + |
| 47 | +## Create a element |
| 48 | + |
| 49 | +You can create a element with `dom.create()`. |
| 50 | + |
| 51 | +```typescript |
| 52 | +dom.create("div"); |
| 53 | +``` |
| 54 | + |
| 55 | +### Set Properties |
| 56 | + |
| 57 | +You can set properties to the element by passing an object to the second parameter. |
| 58 | + |
| 59 | +```typescript |
| 60 | +dom.create("div", { |
| 61 | + id: "id", |
| 62 | + innerText: "Hello World", |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +### Set Styles |
| 67 | + |
| 68 | +You can set styles to the element by different ways. |
| 69 | + |
| 70 | +```typescript |
| 71 | +dom.create("div", { |
| 72 | + css: { |
| 73 | + color: "red", |
| 74 | + backgroundColor: "black", |
| 75 | + }, |
| 76 | +}); |
| 77 | +dom.create("div", { |
| 78 | + css: "color: red; background-color: black;", |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +### Set Attributes |
| 83 | + |
| 84 | +You can set attributes by passing an object to the `attr` key. |
| 85 | + |
| 86 | +```typescript |
| 87 | +dom.create("div", { |
| 88 | + attr: { |
| 89 | + "data-id": "id", |
| 90 | + }, |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +### Set Content |
| 95 | + |
| 96 | +You can set content to the element by setting the `text` or `html` or `val` key. |
| 97 | + |
| 98 | +```typescript |
| 99 | +dom.create("div", { |
| 100 | + text: "Hello World", |
| 101 | +}); |
| 102 | +dom.create("div", { |
| 103 | + html: "<h1>Hello World</h1>", |
| 104 | +}); |
| 105 | +dom.create("input", { |
| 106 | + val: "Hello World", |
| 107 | +}); |
| 108 | +``` |
| 109 | + |
| 110 | +### Set Class |
| 111 | + |
| 112 | +You can set class to the element by setting the `class` key. |
| 113 | + |
| 114 | +```typescript |
| 115 | +dom.create("div", { |
| 116 | + class: "class1 class2", |
| 117 | +}); |
| 118 | +dom.create("div", { |
| 119 | + class: ["class1", "class2"], |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
0 commit comments