|
| 1 | +# FastjsDom API |
| 2 | + |
| 3 | +This page list all the API of FastjsDom. |
| 4 | + |
| 5 | +## FastjsDom.get |
| 6 | + |
| 7 | +Get a property of the element. |
| 8 | + |
| 9 | +```typescript |
| 10 | +dom.select("#id")!.get("innerText"); |
| 11 | +``` |
| 12 | + |
| 13 | +## FastjsDom.set |
| 14 | + |
| 15 | +Set a property of the element. |
| 16 | + |
| 17 | +```typescript |
| 18 | +dom.select("#id")!.set("innerText", "Hello World"); |
| 19 | +``` |
| 20 | + |
| 21 | +## FastjsDom.text |
| 22 | + |
| 23 | +### Get text |
| 24 | + |
| 25 | +Get the text of the element. |
| 26 | + |
| 27 | +```typescript |
| 28 | +dom.select("#id")!.text(); |
| 29 | +``` |
| 30 | + |
| 31 | +### Set text |
| 32 | + |
| 33 | +Set the text of the element. |
| 34 | + |
| 35 | +```typescript |
| 36 | +dom.select("#id")!.text("Hello World"); |
| 37 | +``` |
| 38 | + |
| 39 | +## FastjsDom.html |
| 40 | + |
| 41 | +### Get html |
| 42 | + |
| 43 | +Get the html of the element. |
| 44 | + |
| 45 | +```typescript |
| 46 | +dom.select("#id")!.html(); |
| 47 | +``` |
| 48 | + |
| 49 | +### Set html |
| 50 | + |
| 51 | +Set the html of the element. |
| 52 | + |
| 53 | +```typescript |
| 54 | +dom.select("#id")!.html("<h1>Hello World</h1>"); |
| 55 | +``` |
| 56 | + |
| 57 | +## FastjsDom.val |
| 58 | + |
| 59 | +### Get value |
| 60 | + |
| 61 | +Get the value of the element. |
| 62 | + |
| 63 | +```typescript |
| 64 | +dom.select("#id")!.val(); |
| 65 | +``` |
| 66 | + |
| 67 | +### Set value |
| 68 | + |
| 69 | +Set the value of the element. |
| 70 | + |
| 71 | +```typescript |
| 72 | +dom.select("#id")!.val("Hello World"); |
| 73 | +``` |
| 74 | + |
| 75 | +## FastjsDom.el |
| 76 | + |
| 77 | +Get the element. |
| 78 | + |
| 79 | +```typescript |
| 80 | +dom.select("#id")!.el(); |
| 81 | +``` |
| 82 | + |
| 83 | +## FastjsDom.remove |
| 84 | + |
| 85 | +Remove the element. |
| 86 | + |
| 87 | +```typescript |
| 88 | +dom.select("#id")!.remove(); |
| 89 | +``` |
| 90 | + |
| 91 | +## FastjsDom.focus |
| 92 | + |
| 93 | +Focus the element. |
| 94 | + |
| 95 | +```typescript |
| 96 | +dom.select("#id")!.focus(); |
| 97 | +``` |
| 98 | + |
| 99 | +## FastjsDom.first |
| 100 | + |
| 101 | +Get the first child element. |
| 102 | + |
| 103 | +```typescript |
| 104 | +dom.select("#id")!.first(); |
| 105 | +``` |
| 106 | + |
| 107 | +## FastjsDom.last |
| 108 | + |
| 109 | +Get the last child element. |
| 110 | + |
| 111 | +```typescript |
| 112 | +dom.select("#id")!.last(); |
| 113 | +``` |
| 114 | + |
| 115 | +## FastjsDom.father |
| 116 | + |
| 117 | +Get the father element. |
| 118 | + |
| 119 | +```typescript |
| 120 | +dom.select("#id")!.father(); |
| 121 | +``` |
| 122 | + |
| 123 | +## FastjsDom.children |
| 124 | + |
| 125 | +Get the children elements. Return FastjsDomList. |
| 126 | + |
| 127 | +```typescript |
| 128 | +dom.select("#id")!.children(); |
| 129 | +``` |
| 130 | + |
| 131 | +## FastjsDom.next |
| 132 | + |
| 133 | +Select inside the element. |
| 134 | + |
| 135 | +```typescript |
| 136 | +dom.select("#id")!.next(".class"); |
| 137 | +``` |
| 138 | + |
| 139 | +## FastjsDom.each |
| 140 | + |
| 141 | +Loop the children elements. |
| 142 | + |
| 143 | +```typescript |
| 144 | +dom.select("#id")!.each((element) => { |
| 145 | + console.log(element); |
| 146 | +}); |
| 147 | +// deep loop |
| 148 | +dom.select("#id")!.each((element) => { |
| 149 | + console.log(element); |
| 150 | +}, true); |
| 151 | +``` |
| 152 | + |
| 153 | +## FastjsDom.addEvent |
| 154 | + |
| 155 | +Add event listener to the element. |
| 156 | + |
| 157 | +```typescript |
| 158 | +dom.select("#id")!.addEvent("click", (el: FastjsDom, e: Event) => { |
| 159 | + console.log("click"); |
| 160 | +}); |
| 161 | +``` |
| 162 | + |
| 163 | +## FastjsDom.removeEvent |
| 164 | + |
| 165 | +Remove event listener from the element by key or function. |
| 166 | + |
| 167 | +```typescript |
| 168 | +const fn = (el: FastjsDom, e: Event) => { |
| 169 | + console.log("click"); |
| 170 | +}; |
| 171 | +dom.select("#id")!.addEvent("click", fn); |
| 172 | +// remove by these ways |
| 173 | +dom.select("#id")!.removeEvent("click"); |
| 174 | +dom.select("#id")!.removeEvent("click", 0); |
| 175 | +dom.select("#id")!.removeEvent(fn); |
| 176 | +``` |
| 177 | + |
| 178 | +## FastjsDom.getStyle |
| 179 | + |
| 180 | +Get the style of the element. |
| 181 | + |
| 182 | +```typescript |
| 183 | +dom.select("#id")!.getStyle(); // CSSStyleObject |
| 184 | +dom.select("#id")!.getStyle("color"); // string |
| 185 | +dom.select("#id")!.getStyle((style, dom: FastjsDom) => { |
| 186 | + console.log(style); // CSSStyleObject |
| 187 | +}); |
| 188 | +``` |
| 189 | + |
| 190 | +## FastjsDom.setStyle |
| 191 | + |
| 192 | +Set the style of the element. |
| 193 | + |
| 194 | +```typescript |
| 195 | +dom.select("#id")!.setStyle({ |
| 196 | + color: "red", |
| 197 | + backgroundColor: "black", |
| 198 | +}); |
| 199 | +dom.select("#id")!.setStyle("color", "red"); |
| 200 | +dom.select("#id")!.setStyle("color", "red", true); // important |
| 201 | +``` |
| 202 | + |
| 203 | +## FastjsDom.getClass |
| 204 | + |
| 205 | +Get the class of the element. |
| 206 | + |
| 207 | +```typescript |
| 208 | +dom.select("#id")!.getClass(); // string[] |
| 209 | +dom.select("#id")!.getClass((list: string[], dom: FastjsDom) => { |
| 210 | + console.log(classList); // ["class1", "class2"] |
| 211 | +}); |
| 212 | +``` |
| 213 | + |
| 214 | +## FastjsDom.setClass |
| 215 | + |
| 216 | +Set the class of the element. |
| 217 | + |
| 218 | +```typescript |
| 219 | +dom.select("#id")!.setClass("class", true); // default is true |
| 220 | +dom.select("#id")!.setClass({ |
| 221 | + class1: true, |
| 222 | + class2: false, |
| 223 | +}); |
| 224 | +``` |
| 225 | + |
| 226 | +## FastjsDom.addClass |
| 227 | + |
| 228 | +Add class to the element. |
| 229 | + |
| 230 | +```typescript |
| 231 | +dom.select("#id")!.addClass("class1", "class2"); |
| 232 | +dom.select("#id")!.addClass("class1 class2", "class3"); |
| 233 | +dom.select("#id")!.addClass(["class1", "class2"]); |
| 234 | +``` |
| 235 | + |
| 236 | +## FastjsDom.removeClass |
| 237 | + |
| 238 | +Remove class from the element. |
| 239 | + |
| 240 | +```typescript |
| 241 | +dom.select("#id")!.removeClass("class1", "class2"); |
| 242 | +dom.select("#id")!.removeClass("class1 class2", "class3"); |
| 243 | +dom.select("#id")!.removeClass(["class1", "class2"]); |
| 244 | +``` |
| 245 | + |
| 246 | +## FastjsDom.clearClass |
| 247 | + |
| 248 | +Clear all class from the element. |
| 249 | + |
| 250 | +```typescript |
| 251 | +dom.select("#id")!.clearClass(); |
| 252 | +``` |
| 253 | + |
| 254 | +## FastjsDom.getAttr |
| 255 | + |
| 256 | +Get the attribute of the element. |
| 257 | + |
| 258 | +```typescript |
| 259 | +dom.select("#id")!.getAttr(); // Object |
| 260 | +dom.select("#id")!.getAttr("data-id"); // string |
| 261 | +``` |
| 262 | + |
| 263 | +## FastjsDom.setAttr |
| 264 | + |
| 265 | +Set the attribute of the element. |
| 266 | + |
| 267 | +```typescript |
| 268 | +dom.select("#id")!.setAttr("data-id", "id"); |
| 269 | +dom.select("#id")!.setAttr({ |
| 270 | + "data-id": "id", |
| 271 | +}); |
| 272 | +``` |
| 273 | + |
| 274 | +## FastjsDom.push |
| 275 | + |
| 276 | +Push the element to a target element. |
| 277 | + |
| 278 | +```typescript |
| 279 | +type PushTarget = number |
| 280 | + | "firstElementChild" |
| 281 | + | "lastElementChild" |
| 282 | + | "randomElementChild" |
| 283 | + | "beforeElement" |
| 284 | + | "afterElement" |
| 285 | + | "replaceElement" |
| 286 | + |
| 287 | +function push<T extends PushTarget>( |
| 288 | + el: HTMLElement | FastjsDomList | FastjsDom, |
| 289 | + target: T, |
| 290 | + clone?: boolean |
| 291 | + ): PushReturn<T>; |
| 292 | +``` |
| 293 | + |
| 294 | +Example: |
| 295 | + |
| 296 | +```typescript |
| 297 | +dom.create("div").push(dom.select("#app"), "lastElementChild") |
| 298 | +``` |
| 299 | + |
| 300 | +It will return a `PushReturn<T>` object. |
| 301 | + |
| 302 | +```typescript |
| 303 | +type PushReturn<T> = { |
| 304 | + isReplace: T extends "replaceElement" ? true : false; |
| 305 | + newElement: T extends "replaceElement" ? FastjsDom : never; |
| 306 | + oldElement: T extends "replaceElement" ? FastjsDom : never; |
| 307 | + index: number; |
| 308 | + el: FastjsDom; |
| 309 | + origin: FastjsDom; |
| 310 | + father: FastjsDom | null; |
| 311 | +} |
| 312 | +``` |
| 313 | +
|
| 314 | +## FastjsDom.insert |
| 315 | +
|
| 316 | +Insert a element to it. |
| 317 | +
|
| 318 | +```typescript |
| 319 | +type InsertTarget = number | "after" | "before" | "first" | "last" | "random" |
| 320 | + |
| 321 | +function insert<T extends InsertTarget>( |
| 322 | + el: HTMLElement | FastjsDomList | FastjsDom, |
| 323 | + target: T, |
| 324 | + clone?: boolean |
| 325 | +): InsertReturn; |
| 326 | +``` |
| 327 | + |
| 328 | +Example: |
| 329 | + |
| 330 | +```typescript |
| 331 | +dom.select("#app").insert(dom.create("div"), "last") |
| 332 | +``` |
| 333 | + |
| 334 | +It will return a `InsertReturn` object. |
| 335 | + |
| 336 | +```typescript |
| 337 | +type InsertReturn = { |
| 338 | + index: number; |
| 339 | + added: FastjsDom; |
| 340 | + origin: FastjsDom; |
| 341 | +} |
| 342 | +``` |
| 343 | +
|
0 commit comments