@joyzl/eno 1.1.3 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +465 -35
- package/index.js +145 -42
- package/package.json +1 -1
package/README.md
CHANGED
@@ -7,13 +7,36 @@ npm install @joyzl/eno
|
|
7
7
|
```
|
8
8
|
|
9
9
|
``` javascript
|
10
|
-
import eno from
|
10
|
+
import eno from '@joyzl/eno';
|
11
11
|
```
|
12
12
|
|
13
13
|
## 使用
|
14
14
|
|
15
|
-
### sets
|
16
|
-
|
15
|
+
### eno.sets()
|
16
|
+
|
17
|
+
eno.sets() 将数据对象(通常为JSON实例)设置到HTML标签显示。
|
18
|
+
|
19
|
+
参数形式:
|
20
|
+
* eno.sets(element, parameter);
|
21
|
+
* eno.sets(selector, parameter);
|
22
|
+
* eno.sets(element, selector, parameter);
|
23
|
+
* eno.sets(element, parameter, converter);
|
24
|
+
* eno.sets(element, selector, parameter, converter);
|
25
|
+
|
26
|
+
参数含义:
|
27
|
+
* element 元素实例/元素查找范围;
|
28
|
+
* selector 选择器字符串;
|
29
|
+
* parameter 数据对象/数据对象数组;
|
30
|
+
* converter 转换函数 function(element, parameter, name){};
|
31
|
+
|
32
|
+
转换函数参数含义:
|
33
|
+
* element 元素实例;
|
34
|
+
* parameter 对象实例;
|
35
|
+
* name 情形值;
|
36
|
+
|
37
|
+
#### 单个对象设置到HTML显示
|
38
|
+
|
39
|
+
将单个数据对象(例如从服务器获取的JSON对象)设置到HTML元素以显示数据。
|
17
40
|
|
18
41
|
首先建立HTML标签元素如下所示:
|
19
42
|
```html
|
@@ -41,9 +64,193 @@ eno.sets(div, user);
|
|
41
64
|
</div>
|
42
65
|
```
|
43
66
|
|
44
|
-
|
45
|
-
|
46
|
-
|
67
|
+
#### 多个对象设置到HTML显示
|
68
|
+
|
69
|
+
将多个数据对象(例如从服务器获取的JSON对象数组)设置到HTML元素以显示数据;
|
70
|
+
此时eno将自动克隆创建对应数量的HTML元素以对应多项数据。
|
71
|
+
|
72
|
+
首先建立HTML标签元素如下所示:
|
73
|
+
```html
|
74
|
+
<div id="users">
|
75
|
+
<div>
|
76
|
+
<span name="realname"></span>
|
77
|
+
<span name="birthday"></span>
|
78
|
+
</div>
|
79
|
+
</div>
|
80
|
+
```
|
81
|
+
|
82
|
+
将数据对象设置到HTML标签元素。
|
83
|
+
```javascript
|
84
|
+
let users = [{
|
85
|
+
realname: "小明",
|
86
|
+
birthday: "1992-5-27"
|
87
|
+
},
|
88
|
+
{
|
89
|
+
realname: "小王",
|
90
|
+
birthday: "1993-8-16"
|
91
|
+
}
|
92
|
+
];
|
93
|
+
eno.sets("#users", users);
|
94
|
+
```
|
95
|
+
|
96
|
+
数据对象的字段值将根据HTML标签元素指定的name属性插入到标签中;
|
97
|
+
此时eno将 "#users" DIV 的子元素作为模板,自动克隆以显示多项数据。
|
98
|
+
```html
|
99
|
+
<div id="users">
|
100
|
+
<div>
|
101
|
+
<span name="realname">小明</span>
|
102
|
+
<span name="birthday">1992-5-27</span>
|
103
|
+
</div>
|
104
|
+
<div>
|
105
|
+
<span name="realname">小王</span>
|
106
|
+
<span name="birthday">1993-8-16</span>
|
107
|
+
</div>
|
108
|
+
</div>
|
109
|
+
```
|
110
|
+
|
111
|
+
在相同元素实例多次执行eno.sets方法时,eno将自动维护对应的数量的子元素,以保持正确的显示;
|
112
|
+
如果传入的对象数组为[]或null这将清除所有子元素,eno会通过隐藏以保留原始模板。
|
113
|
+
|
114
|
+
#### 多个对象设置到HTML显示并指定模板
|
115
|
+
|
116
|
+
通过模板<template>显示多个对象到HTML中具有更好的灵活性,eno将保留模板标签之外的HTML。
|
117
|
+
```html
|
118
|
+
<div id="users">
|
119
|
+
<div>
|
120
|
+
<span>用户列表</span>
|
121
|
+
</div>
|
122
|
+
<template>
|
123
|
+
<div>
|
124
|
+
<span name="realname"></span>
|
125
|
+
<span name="birthday"></span>
|
126
|
+
</div>
|
127
|
+
</template>
|
128
|
+
</div>
|
129
|
+
```
|
130
|
+
|
131
|
+
执行eno.sets("#users",users)将得到如下HTML实例:
|
132
|
+
```html
|
133
|
+
<div id="users">
|
134
|
+
<div>
|
135
|
+
<span>用户列表</span>
|
136
|
+
</div>
|
137
|
+
<template>
|
138
|
+
<div>
|
139
|
+
<span name="realname"></span>
|
140
|
+
<span name="birthday"></span>
|
141
|
+
</div>
|
142
|
+
</template>
|
143
|
+
<div>
|
144
|
+
<span name="realname">小明</span>
|
145
|
+
<span name="birthday">1992-5-27</span>
|
146
|
+
</div>
|
147
|
+
<div>
|
148
|
+
<span name="realname">小王</span>
|
149
|
+
<span name="birthday">1993-8-16</span>
|
150
|
+
</div>
|
151
|
+
</div>
|
152
|
+
```
|
153
|
+
eno根据"#users"中的<template>创建对应数量的HTML元素显示数据,
|
154
|
+
并保留其它HTML标签(也保留<template>本身,因为此标签不会被显示)。
|
155
|
+
|
156
|
+
#### 在HTML指示对象字段
|
157
|
+
|
158
|
+
将对象通过HTML显示时,eno通过标签的"name"属性值确定对象字段(区分大小写);
|
159
|
+
属性值支持点语法分隔多级字段,例如:"department.name";
|
160
|
+
|
161
|
+
eno确定对象字段后将根据标签类型执行默认的值设置:
|
162
|
+
* input标签设置为value属性值;
|
163
|
+
* img标签设置为src属性值;
|
164
|
+
* 其它标签设置为内部文本(innerText);
|
165
|
+
|
166
|
+
```html
|
167
|
+
<div id="users">
|
168
|
+
<template>
|
169
|
+
<div>
|
170
|
+
<img name="avatar"/>
|
171
|
+
<span name="realname"></span>
|
172
|
+
<span name="birthday"></span>
|
173
|
+
<span name="department.name"></span>
|
174
|
+
</div>
|
175
|
+
</template>
|
176
|
+
</div>
|
177
|
+
```
|
178
|
+
```javascript
|
179
|
+
let users = [{
|
180
|
+
realname: "小明",
|
181
|
+
birthday: "1992-5-27",
|
182
|
+
avatar: "okjniybu.png",
|
183
|
+
department: {
|
184
|
+
name: "A部"
|
185
|
+
}
|
186
|
+
},
|
187
|
+
{
|
188
|
+
realname: "小王",
|
189
|
+
birthday: "1993-8-16",
|
190
|
+
avatar: "likjudde.png",
|
191
|
+
department: {
|
192
|
+
name: "B部"
|
193
|
+
}
|
194
|
+
}
|
195
|
+
];
|
196
|
+
eno.sets("#users", users);
|
197
|
+
```
|
198
|
+
|
199
|
+
#### 自定义对象字段与HTML显示方式
|
200
|
+
|
201
|
+
如果默认行为无法满足显示需求,例如根据状态字段更改颜色,
|
202
|
+
可通过"case"属性值和自定义转换函数执行特殊处理;
|
203
|
+
```html
|
204
|
+
<div id="users">
|
205
|
+
<template>
|
206
|
+
<div>
|
207
|
+
<img case="url"/>
|
208
|
+
<span name="realname"></span>
|
209
|
+
<span name="birthday"></span>
|
210
|
+
<span name="department.name"></span>
|
211
|
+
</div>
|
212
|
+
</template>
|
213
|
+
</div>
|
214
|
+
```
|
215
|
+
转换函数将在遇到的每一个"case"属性时触发,并传入三个参数
|
216
|
+
分别为element表示"case"所在的元素实例,
|
217
|
+
parameter表示当前数据对象,
|
218
|
+
name指示"case"的值;
|
219
|
+
```javascript
|
220
|
+
let users = [...];
|
221
|
+
eno.sets("#users", users, function(element, parameter, name){
|
222
|
+
if(name=="url"){
|
223
|
+
element.src = "http://joyzl.net/" + parameter.avatar;
|
224
|
+
}
|
225
|
+
});
|
226
|
+
```
|
227
|
+
通过转换函数可以执行任何需要的自定义处理;
|
228
|
+
但须注意的是,每个模板实例中的多个"case"都将调用转换函数,
|
229
|
+
如果同时指定"name"属性,将在调用转换函数后执行默认行为。
|
230
|
+
|
231
|
+
### eno.gets()
|
232
|
+
|
233
|
+
从HTML元素获取用户数据以形成JSON对象实例。
|
234
|
+
与eno.sets方法的行为正好相反,将从HTML中具有"name"属性的标签提取数据。
|
235
|
+
|
236
|
+
参数形式:
|
237
|
+
* eno.gets(element);
|
238
|
+
* eno.gets(selector);
|
239
|
+
* eno.gets(element, converter);
|
240
|
+
* eno.gets(selector, converter);
|
241
|
+
* eno.gets(element, selector, converter);
|
242
|
+
|
243
|
+
参数含义:
|
244
|
+
* element 元素实例;
|
245
|
+
* selector 选择器字符串;
|
246
|
+
* converter 转换函数 function(element, parameter, name){};
|
247
|
+
|
248
|
+
转换函数参数含义:
|
249
|
+
* element 元素实例;
|
250
|
+
* parameter 对象实例;
|
251
|
+
* name 情形值;
|
252
|
+
|
253
|
+
#### 从HTML中提取数据
|
47
254
|
|
48
255
|
假设有以下HTML实例:
|
49
256
|
```html
|
@@ -56,11 +263,15 @@ eno.sets(div, user);
|
|
56
263
|
执行gets方法获取数据对象:
|
57
264
|
```javascript
|
58
265
|
let values = eno.gets(element);
|
59
|
-
//
|
60
|
-
//
|
61
|
-
//
|
62
|
-
//
|
266
|
+
// values 实例字段如下
|
267
|
+
// {
|
268
|
+
// realname: "小明",
|
269
|
+
// birthday: "1992-5-27"
|
270
|
+
// }
|
63
271
|
```
|
272
|
+
eno.gets将普通标签获取内部普通文本内容(innetText)。
|
273
|
+
|
274
|
+
#### 从表单中提取数据
|
64
275
|
|
65
276
|
获取表单输入值,gets将识别传入参数是否表单。
|
66
277
|
```html
|
@@ -73,52 +284,271 @@ let values = eno.gets(element);
|
|
73
284
|
执行gets方法获取form数据对象:
|
74
285
|
```javascript
|
75
286
|
let values = eno.gets(element);
|
76
|
-
//
|
77
|
-
//
|
78
|
-
//
|
79
|
-
//
|
287
|
+
// values 实例字段如下
|
288
|
+
// {
|
289
|
+
// realname: "小张",
|
290
|
+
// birthday: "1982-10-30"
|
291
|
+
// }
|
80
292
|
```
|
293
|
+
eno.gets将表单标签获取控件值内容(control.value)。
|
294
|
+
|
295
|
+
#### 自定义数据提取方式
|
81
296
|
|
297
|
+
与eno.sets方法相同,eno.gets也支持同样参数的数据转换函数用于自定义提取方式。
|
298
|
+
```javascript
|
299
|
+
let values = eno.gets(element, function(element, parameter, name){
|
300
|
+
if(name=="url"){
|
301
|
+
parameter.avatar = element.src.replace("http://joyzl.net/","");
|
302
|
+
}
|
303
|
+
});
|
304
|
+
// values 实例字段如下
|
305
|
+
// {
|
306
|
+
// realname: "小张",
|
307
|
+
// birthday: "1982-10-30",
|
308
|
+
// avatar: "okjniybu.png"
|
309
|
+
// }
|
310
|
+
```
|
82
311
|
|
83
|
-
###
|
84
|
-
|
312
|
+
### eno.create()
|
313
|
+
|
314
|
+
eno.create() 用于将字符串形式的HTML标签创建为HTML实例。
|
315
|
+
|
316
|
+
参数形式:
|
317
|
+
* eno.create(html);
|
318
|
+
|
319
|
+
参数含义:
|
320
|
+
* html 字符串;
|
85
321
|
|
86
322
|
```javascript
|
87
|
-
|
88
|
-
|
323
|
+
let element = eno.create("<div><a href='http://www.joyzl.com'>joyzl</a></div>");
|
324
|
+
```
|
89
325
|
|
90
|
-
|
91
|
-
|
92
|
-
|
326
|
+
注意:此时创建的标签实例未插入文档中,因此不会在浏览器网页呈现。
|
327
|
+
|
328
|
+
### eno.append()
|
329
|
+
|
330
|
+
eno.append() 用于将字符串形式的HTML标签创建为HTML实例,并插入文档指定的位置尾部。
|
331
|
+
|
332
|
+
参数形式:
|
333
|
+
* eno.append(element, html);
|
334
|
+
* eno.append(selector, html);
|
335
|
+
|
336
|
+
参数含义:
|
337
|
+
* element 元素实例;
|
338
|
+
* selector 选择器字符串;
|
339
|
+
* html 字符串;
|
340
|
+
|
341
|
+
```javascript
|
342
|
+
// 添加到<body>中尾部
|
343
|
+
let element1 = eno.append("<div><a href='http://www.joyzl.com'>joyzl</a></div>");
|
344
|
+
|
345
|
+
// 添加到指定元素中尾部
|
346
|
+
let parent = document.getElementById("main");
|
347
|
+
let element2 = eno.append(parent, "<div><a href='http://www.joyzl.com'>joyzl</a></div>");
|
93
348
|
```
|
94
349
|
|
95
|
-
###
|
96
|
-
|
350
|
+
### eno.replace()
|
351
|
+
|
352
|
+
eno.replace() 用于将字符串形式的HTML标签创建为HTML实例,并替换指定的元素。
|
353
|
+
|
354
|
+
参数形式:
|
355
|
+
* eno.replace(element, html);
|
356
|
+
* eno.replace(selector, html);
|
357
|
+
|
358
|
+
参数含义:
|
359
|
+
* element 元素实例;
|
360
|
+
* selector 选择器字符串;
|
361
|
+
* html 字符串;
|
97
362
|
|
98
|
-
在整个文档实例范围中查找:
|
99
363
|
```javascript
|
100
|
-
let element =
|
364
|
+
let element = document.getElementById("customer-define");
|
365
|
+
element = eno.replace(element, "<div><a href='http://www.joyzl.com'>joyzl</a></div>");
|
101
366
|
```
|
102
367
|
|
103
|
-
|
368
|
+
### eno.select()
|
369
|
+
|
370
|
+
eno.select() 用于在文档或指定范围查找元素。
|
371
|
+
|
372
|
+
参数形式:
|
373
|
+
* eno.select(selector);
|
374
|
+
* eno.select(element, selector);
|
375
|
+
|
376
|
+
参数含义:
|
377
|
+
* element 元素实例;
|
378
|
+
* selector 选择器字符串;
|
379
|
+
|
104
380
|
```javascript
|
105
|
-
|
106
|
-
let
|
381
|
+
// 在整个文档中查找
|
382
|
+
let element = eno.select("#id");
|
383
|
+
|
384
|
+
// 在指定元素中查找
|
385
|
+
element = eno.select(element, "div");
|
107
386
|
```
|
108
387
|
|
388
|
+
支持标准的CSS选择器字符串,如果匹配多个元素将以数组返回结果集,仅匹配单个时仅返回匹配的元素实例。
|
389
|
+
|
390
|
+
### eno.remove()
|
391
|
+
|
392
|
+
eno.remove() 用于移除文档中的指定元素。
|
393
|
+
|
394
|
+
参数形式:
|
395
|
+
* eno.remove(element);
|
396
|
+
* eno.remove(selector);
|
397
|
+
* eno.remove(element, selector);
|
398
|
+
|
399
|
+
参数含义:
|
400
|
+
* element 元素实例;
|
401
|
+
* selector 选择器字符串;
|
402
|
+
|
403
|
+
### eno.hide()
|
404
|
+
|
405
|
+
eno.hide() 用于隐藏元素。
|
406
|
+
|
407
|
+
参数形式:
|
408
|
+
* eno.hide(element);
|
409
|
+
* eno.hide(selector);
|
410
|
+
* eno.hide(element, selector);
|
411
|
+
|
412
|
+
参数含义:
|
413
|
+
* element 元素实例;
|
414
|
+
* selector 选择器字符串;
|
415
|
+
|
416
|
+
### eno.show()
|
417
|
+
|
418
|
+
eno.show() 用于显示元素。
|
419
|
+
|
420
|
+
参数形式:
|
421
|
+
* eno.show(element);
|
422
|
+
* eno.show(selector);
|
423
|
+
* eno.show(element, selector);
|
424
|
+
|
425
|
+
参数含义:
|
426
|
+
* element 元素实例;
|
427
|
+
* selector 选择器字符串;
|
428
|
+
|
429
|
+
### eno.toggle()
|
430
|
+
|
431
|
+
eno.toggle() 用于切换元素显示或样式类。
|
432
|
+
调用此方法仅指定元素或选择器参数,指定或匹配的元素将显示,其余同级元素全部隐藏;
|
433
|
+
如果指定了样式类参数(applyClass, otherClass),则匹配的元素将应用applyClass,其余同级元素应用otherClass样式类。
|
109
434
|
|
110
|
-
|
111
|
-
|
435
|
+
参数形式:
|
436
|
+
* eno.toggle(element);
|
437
|
+
* eno.toggle(selector);
|
438
|
+
* eno.toggle(element, selector);
|
439
|
+
* eno.toggle(element, applyClass, otherClass);
|
440
|
+
* eno.toggle(element, selector, applyClass, otherClass);
|
112
441
|
|
442
|
+
参数含义:
|
443
|
+
* element 元素实例;
|
444
|
+
* selector 选择器字符串;
|
445
|
+
* applyClass 当前元素应用的样式类;
|
446
|
+
* otherClass 其它元素应用的样式类;
|
113
447
|
|
114
|
-
|
448
|
+
此功能可用于层叠卡片显示切换或选中项高亮。
|
449
|
+
|
450
|
+
|
451
|
+
|
452
|
+
### eno.bind()
|
453
|
+
|
454
|
+
eno.bind() 用于绑定元素的事件。
|
455
|
+
|
456
|
+
参数形式:
|
457
|
+
* eno.bind(element, eventName, listener);
|
458
|
+
* eno.bind(selector, eventName, listener);
|
459
|
+
* eno.bind(element, selector, eventName, listener);
|
460
|
+
|
461
|
+
参数含义:
|
462
|
+
* element 元素实例;
|
463
|
+
* selector 选择器字符串;
|
464
|
+
* eventName 事件名称 "click"、"submit" ...;
|
465
|
+
* listener 事件处理函数 function(event){};
|
466
|
+
|
467
|
+
处理函数参数含义:
|
468
|
+
* this 元素实例;
|
469
|
+
* event 事件实例;
|
470
|
+
|
471
|
+
此方法的事件绑定与DOM API并无任何差异,其行为与Element.addEventListener()完全相同。
|
472
|
+
|
473
|
+
#### eno.sets()的事件处理
|
474
|
+
|
475
|
+
对于通过eno.sets()方法生成并填充值的元素,如果要监听每个元素的事件,
|
476
|
+
可通过eno.bind()配合eno.entity()和eno.element()方法优化事件处理,而无须为每个子元素绑定事件;
|
477
|
+
|
478
|
+
具体方法如下:
|
479
|
+
+ 容器绑定所需事件;
|
480
|
+
+ 在处理函数中通过eno.element()获取目标元素实例;
|
481
|
+
+ 在处理函数中通过eno.entity()获取目标对象实例;
|
482
|
+
|
483
|
+
示例代码:
|
115
484
|
```javascript
|
116
|
-
|
485
|
+
const element = eno.append(`
|
486
|
+
<div id="users">
|
487
|
+
<div>
|
488
|
+
<span>用户列表</span>
|
489
|
+
</div>
|
490
|
+
<template>
|
491
|
+
<div>
|
492
|
+
<span name="realname"></span>
|
493
|
+
<span name="birthday"></span>
|
494
|
+
</div>
|
495
|
+
</template>
|
496
|
+
</div>
|
497
|
+
`);
|
498
|
+
eno.bind(element,"click",function(event){
|
499
|
+
// 获取数据对象实例
|
500
|
+
eno.entity(event)
|
501
|
+
|
502
|
+
// 获取元素对象实例
|
503
|
+
eno.element(event);
|
504
|
+
});
|
505
|
+
eno.sets(element,data);
|
117
506
|
```
|
118
507
|
|
508
|
+
### eno.entity()
|
509
|
+
|
510
|
+
eno.entity() 用于获取通过eno.sets()方法填充元素的数据对象。
|
511
|
+
|
512
|
+
参数形式:
|
513
|
+
* eno.entity(event);
|
514
|
+
* eno.entity(element);
|
515
|
+
* eno.entity(element, selector);
|
516
|
+
|
517
|
+
参数含义:
|
518
|
+
* event 事件实例;
|
519
|
+
* element 元素实例;
|
520
|
+
* selector 选择器字符串;
|
521
|
+
|
522
|
+
### eno.element()
|
523
|
+
|
524
|
+
eno.element() 用于获取通过eno.sets()方法填充元素的实例。
|
525
|
+
|
526
|
+
参数形式:
|
527
|
+
* eno.element(event);
|
528
|
+
|
529
|
+
参数含义:
|
530
|
+
* event 事件实例;
|
531
|
+
|
532
|
+
### eno.query()
|
533
|
+
|
534
|
+
eno.query() 用于获取指定URL或当前URL中的查询字符串参数。
|
535
|
+
|
536
|
+
参数形式:
|
537
|
+
* eno.query();
|
538
|
+
* eno.query(url);
|
539
|
+
* eno.query(name);
|
540
|
+
* eno.query(url, name);
|
541
|
+
|
542
|
+
参数含义:
|
543
|
+
* url 字符串,如果未指定则默认当前页面URL;
|
544
|
+
* name 参数名字符串,区分大小写;
|
545
|
+
|
119
546
|
|
120
|
-
在指定元素实例范围中查找:
|
121
547
|
```javascript
|
122
|
-
|
123
|
-
let
|
548
|
+
// 从当前URL获取参数
|
549
|
+
let token = eno.query("token");
|
550
|
+
|
551
|
+
// 从指定URL获取参数
|
552
|
+
let url = "http://www.joyzl.net/test?name=value";
|
553
|
+
let value = eno.query(url, "name");
|
124
554
|
```
|
package/index.js
CHANGED
@@ -6,6 +6,7 @@ export default {
|
|
6
6
|
create,
|
7
7
|
append,
|
8
8
|
replace,
|
9
|
+
selects,
|
9
10
|
select,
|
10
11
|
remove,
|
11
12
|
|
@@ -134,7 +135,7 @@ function replace(element, html) {
|
|
134
135
|
* 在指定范围内/整个文档查找
|
135
136
|
* @param {Element} element 标签元素
|
136
137
|
* @param {String} selector 筛选字符
|
137
|
-
* @return {Element}
|
138
|
+
* @return {Element} 匹配的单个标签元素
|
138
139
|
*/
|
139
140
|
function select(element, selector) {
|
140
141
|
if (arguments.length == 1) {
|
@@ -196,6 +197,65 @@ function select(element, selector) {
|
|
196
197
|
}
|
197
198
|
}
|
198
199
|
|
200
|
+
/**
|
201
|
+
* 在指定范围内/整个文档查找
|
202
|
+
* @param {Element} element 标签元素
|
203
|
+
* @param {String} selector 筛选字符
|
204
|
+
* @return {[Element]} 匹配的多个标签元素
|
205
|
+
*/
|
206
|
+
function selects(element, selector) {
|
207
|
+
if (arguments.length == 1) {
|
208
|
+
// 仅指定1个参数
|
209
|
+
// selects(element/selector);
|
210
|
+
if (element.trim) {
|
211
|
+
// 仅提供字符串参数
|
212
|
+
element = document.querySelectorAll(element);
|
213
|
+
if (element.length == 0) {
|
214
|
+
return null;
|
215
|
+
}
|
216
|
+
return Array.from(element);
|
217
|
+
} else
|
218
|
+
if (element.nodeType) {
|
219
|
+
// 仅提供元素参数
|
220
|
+
return [element];
|
221
|
+
}
|
222
|
+
} else
|
223
|
+
if (arguments.length == 2) {
|
224
|
+
// 指定了2个参数
|
225
|
+
// select(element, selector);
|
226
|
+
if (element.trim) {
|
227
|
+
element = document.querySelectorAll(element);
|
228
|
+
if (element.length == 0) {
|
229
|
+
return null;
|
230
|
+
}
|
231
|
+
} else
|
232
|
+
if (element.nodeType) {
|
233
|
+
element = element.querySelectorAll(selector);
|
234
|
+
if (element.length == 0) {
|
235
|
+
return null;
|
236
|
+
}
|
237
|
+
return Array.from(element);
|
238
|
+
} else
|
239
|
+
if (Array.isArray(element)) {
|
240
|
+
if (element.length == 0) {
|
241
|
+
return null;
|
242
|
+
}
|
243
|
+
// element[]
|
244
|
+
let nodes, items = new Array();
|
245
|
+
for (let i = 0; i < element.length; i++) {
|
246
|
+
nodes = element[i].querySelectorAll(selector);
|
247
|
+
for (let n = 0; n < nodes.length; n++) {
|
248
|
+
items.push(nodes[n]);
|
249
|
+
}
|
250
|
+
}
|
251
|
+
if (items.length == 0) {
|
252
|
+
return null;
|
253
|
+
}
|
254
|
+
return items;
|
255
|
+
}
|
256
|
+
}
|
257
|
+
}
|
258
|
+
|
199
259
|
/**
|
200
260
|
* 从文档移除
|
201
261
|
* @param {Element} element 标签元素
|
@@ -363,6 +423,7 @@ function defaultConverter(element, parameter, name) {}
|
|
363
423
|
function gets(element, selector, converter = defaultConverter) {
|
364
424
|
if (arguments.length == 1) {
|
365
425
|
// gets(element)
|
426
|
+
// gets(selector)
|
366
427
|
element = select(element);
|
367
428
|
} else
|
368
429
|
if (arguments.length == 2) {
|
@@ -412,6 +473,7 @@ function gets(element, selector, converter = defaultConverter) {
|
|
412
473
|
function sets(element, selector, parameter, converter = defaultConverter) {
|
413
474
|
if (arguments.length == 2) {
|
414
475
|
// sets(element,parameter)
|
476
|
+
// sets(selector,parameter)
|
415
477
|
element = select(element);
|
416
478
|
parameter = selector;
|
417
479
|
} else
|
@@ -587,7 +649,7 @@ function get(element, parameter, converter) {
|
|
587
649
|
}
|
588
650
|
name = element.getAttribute("name");
|
589
651
|
if (name && name.length) {
|
590
|
-
if (element.
|
652
|
+
if (element.type) {
|
591
653
|
valu(parameter, name, element.value);
|
592
654
|
} else
|
593
655
|
if (element.src) {
|
@@ -617,7 +679,7 @@ function set(element, parameter, converter) {
|
|
617
679
|
}
|
618
680
|
name = element.getAttribute("name");
|
619
681
|
if (name && name.length) {
|
620
|
-
if (element.
|
682
|
+
if (element.type) {
|
621
683
|
element.value = text(vale(parameter, name));
|
622
684
|
} else
|
623
685
|
if (element.src) {
|
@@ -768,29 +830,6 @@ function entity(e, selector) {
|
|
768
830
|
return null;
|
769
831
|
}
|
770
832
|
|
771
|
-
/**
|
772
|
-
* 根据事件或元素获取属性指定的动作
|
773
|
-
* @param {Event} e
|
774
|
-
* @param {String} a
|
775
|
-
*/
|
776
|
-
function action(e, a) {
|
777
|
-
if (e.target) {
|
778
|
-
e = e.target;
|
779
|
-
} else
|
780
|
-
if (e.srcElement) {
|
781
|
-
e = e.srcElement;
|
782
|
-
}
|
783
|
-
|
784
|
-
while (e) {
|
785
|
-
if (e.hasAttribute(a)) {
|
786
|
-
return true;
|
787
|
-
} else {
|
788
|
-
e = e.parentElement;
|
789
|
-
}
|
790
|
-
}
|
791
|
-
return false;
|
792
|
-
}
|
793
|
-
|
794
833
|
/**
|
795
834
|
* 根据事件获取绑定实体的元素
|
796
835
|
* @param {Event} e
|
@@ -820,16 +859,30 @@ function element(e) {
|
|
820
859
|
* @return {String} 参数值
|
821
860
|
*/
|
822
861
|
function query(url, name) {
|
823
|
-
if (arguments.length ==
|
824
|
-
//
|
825
|
-
name = url;
|
862
|
+
if (arguments.length == 0) {
|
863
|
+
// query()
|
826
864
|
// window.location.search 返回从问号?开始的URL查询部分
|
827
|
-
// ?name1=value1&name2=value2
|
828
865
|
url = window.location.search;
|
829
|
-
}
|
866
|
+
} else
|
867
|
+
if (arguments.length == 1) {
|
868
|
+
// query(url)
|
869
|
+
// query(name)
|
870
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
871
|
+
let index = url.indexOf("?");
|
872
|
+
if (index > 0) {
|
873
|
+
url = url.substring(index);
|
874
|
+
} else {
|
875
|
+
return null;
|
876
|
+
}
|
877
|
+
} else {
|
878
|
+
name = url;
|
879
|
+
// window.location.search 返回从问号?开始的URL查询部分
|
880
|
+
url = window.location.search;
|
881
|
+
}
|
882
|
+
} else
|
830
883
|
if (arguments.length == 2) {
|
831
|
-
//
|
832
|
-
let index = url.
|
884
|
+
// query(url, name)
|
885
|
+
let index = url.indexOf("?");
|
833
886
|
if (index > 0) {
|
834
887
|
url = url.substring(index);
|
835
888
|
} else {
|
@@ -838,18 +891,68 @@ function query(url, name) {
|
|
838
891
|
}
|
839
892
|
|
840
893
|
if (url) {
|
841
|
-
|
842
|
-
|
843
|
-
start
|
844
|
-
|
845
|
-
start
|
846
|
-
|
847
|
-
|
848
|
-
|
894
|
+
if (name) {
|
895
|
+
let start = url.indexOf(name);
|
896
|
+
if (start >= 0) {
|
897
|
+
start += name.length;
|
898
|
+
if (url.charAt(start) == '=') {
|
899
|
+
start++;
|
900
|
+
let end = url.indexOf('&', start);
|
901
|
+
if (end >= 0) {
|
902
|
+
return url.substring(start, end);
|
903
|
+
}
|
904
|
+
return url.substring(start);
|
849
905
|
}
|
850
|
-
return url.substring(start);
|
851
906
|
}
|
907
|
+
} else {
|
908
|
+
// ?name1=value1&name2=value2
|
909
|
+
let start = 1;
|
910
|
+
let index = 1;
|
911
|
+
let name, parameter = {};
|
912
|
+
while (index >= 0 && index < url.length) {
|
913
|
+
start = url.indexOf("=", index);
|
914
|
+
if (start >= 0) {
|
915
|
+
name = url.substring(start, index);
|
916
|
+
start = ++index;
|
917
|
+
|
918
|
+
index = url.indexOf("&", index);
|
919
|
+
if (index >= 0) {
|
920
|
+
parameter[name] = url.substring(start, index);
|
921
|
+
start = ++index;
|
922
|
+
} else {
|
923
|
+
parameter[name] = url.substring(start);
|
924
|
+
break;
|
925
|
+
}
|
926
|
+
} else {
|
927
|
+
break;
|
928
|
+
}
|
929
|
+
}
|
930
|
+
return parameter;
|
852
931
|
}
|
853
932
|
}
|
854
933
|
return null;
|
934
|
+
}
|
935
|
+
|
936
|
+
/**
|
937
|
+
* 根据事件或元素获取属性指定的动作
|
938
|
+
* @param {Event} e
|
939
|
+
* @param {String} a
|
940
|
+
* @deprecated 1.1.3
|
941
|
+
*/
|
942
|
+
function action(e, a) {
|
943
|
+
if (e.target) {
|
944
|
+
e = e.target;
|
945
|
+
} else
|
946
|
+
if (e.srcElement) {
|
947
|
+
e = e.srcElement;
|
948
|
+
}
|
949
|
+
|
950
|
+
while (e) {
|
951
|
+
if (e.hasAttribute(a)) {
|
952
|
+
return true;
|
953
|
+
} else {
|
954
|
+
e = e.parentElement;
|
955
|
+
}
|
956
|
+
}
|
957
|
+
return false;
|
855
958
|
}
|