Html 助手
/*!
* Html 助手
* version: 1.0.0-2018.07.25
* Requires ES6
* Copyright (c) 2018 Tiac
* http://www.cnblogs.com/tujia/p/9369027.html
*/ class Html
{
static merge(){
let obj = {};
for(let i in arguments){
let options = arguments[i];
for(let key in options){
if(options[key]) obj[key] = options[key];
}
} return obj;
} static tag(type, value = '', options={}){
type = type.toLowerCase();
let ele = document.createElement(type);
for(var key in options){
ele.setAttribute(key, options[key]);
}
if(value!='') ele.value = value; return ele;
} static a(text, url = '', options = {})
{
if (url != '') {
options['href'] = url;
} let ele = this.tag('a', '', options);
ele.innerHTML = text; return ele;
} static img(src, options = {})
{
options = this.merge({'src':src}, options); if (options['srcset']!=undefined && typeof(options['srcset'])=='object') {
let srcset = [];
for(let key in options['srcset']){
srcset.push(options['srcset'][key]+' '+key);
}
options['srcset'] = srcset.join(',');
} if (options['alt']==undefined) {
options['alt'] = '';
} return this.tag('img', '', options);
} static label(content, _for = '', options = {})
{
options = this.merge({'for':_for}, options); let ele = this.tag('label', '', options);
ele.innerHTML = content; return ele;
} static button(content = 'Button', options = {})
{
options = this.merge({'type':'button'}, options); let ele = this.tag('button', '', options);
ele.innerHTML = content; return ele;
} static submitButton(content = 'Submit', options = {})
{
options = this.merge({'type':'submit'}, options); let ele = this.tag('button', '', options);
ele.innerHTML = content; return ele;
} static resetButton(content = 'Reset', options = {})
{
options = this.merge({'type':'reset'}, options); let ele = this.tag('button', '', options);
ele.innerHTML = content; return ele;
} static input(type, name = '', value = '', options = {})
{
options = this.merge({
'type': type,
'name': name
}, options); return this.tag('input', value, options);
} static buttonInput(label = 'Button', options = {})
{
options = this.merge({'type':'button'}, options); return this.tag('input', label, options);
} static submitInput(label = 'Submit', options = {})
{
options = this.merge({'type':'submit'}, options); return this.tag('input', label, options);
} static resetInput(label = 'Reset', options = {})
{
options = this.merge({'type':'reset'}, options); return this.tag('input', label, options);
} static textInput(name = '', value = '', options = {})
{
return this.input('text', name, value, options);
} static hiddenInput(name, value = '', options = {})
{
return this.input('hidden', name, value, options);
} static passwordInput(name, value = '', options = {})
{
return this.input('password', name, value, options);
} static fileInput(name, value = '', options = {})
{
return this.input('file', name, value, options);
} static textarea(name, value = '', options = {})
{
options = this.merge({'name':name}, options); return this.tag('textarea', value, options);
} static radio(name, checked = false, options = {})
{
return this.booleanInput('radio', name, checked, options);
} static checkbox(name, checked = false, options = {})
{
return this.booleanInput('checkbox', name, checked, options);
} static booleanInput(type, name, checked = false, options = {})
{
let label = '';
let lableOptions = {};
if(options.label!=undefined){
label = options.label;
delete(options.label);
}
if(options.lableOptions!=undefined){
lableOptions = options.lableOptions;
delete(options.lableOptions);
}
let pluginOptions = {};
if(options.pluginOptions!=undefined){
pluginOptions = options.pluginOptions;
delete(options.pluginOptions);
} let ele = this.input(type, name, '', options);
ele.checked = checked; if(typeof($)=='function'){
window.setTimeout(()=>{
if(typeof($(ele).iCheck)=='function'){
pluginOptions = this.merge({
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue',
increaseArea: '20%' // optional
}, pluginOptions);
$(ele).iCheck(pluginOptions);
}
}, 500);
} if(label!=''){
let oLabel = this.label(label, '', lableOptions);
oLabel.prepend(ele);
return oLabel;
}else{
return ele;
}
} static dropDownList(name, selection = '', items = {}, options = {})
{
let pluginOptions = {};
if(options.pluginOptions!=undefined){
pluginOptions = options.pluginOptions;
delete(options.pluginOptions);
} let ele = this.tag('select', '', options);
let opts = '';
for(let key in items){
opts += `<option value="${key}">${items[key]}</option>`;
}
ele.innerHTML = opts; if(options.multiple!=undefined)
{
if(typeof(selection)=='object') selection = JSON.stringify(selection);
selection = selection.replace(/\[(.+)\]/, '$1') + ','; ele.querySelectorAll('option').forEach((item, i)=>{
if(selection.indexOf(item.getAttribute('value')+',')>-1){
item.selected = true;
}
});
}
else
{
ele.value = selection;
} if(typeof($)=='function'){
window.setTimeout(()=>{
if(typeof($(ele).select2)=='function'){
$(ele).select2(pluginOptions);
}
}, 500);
} return ele;
}
} export default Html;
Html 助手的更多相关文章
- Markdown 图片助手-MarkdownPicPicker
title: Markdown 图片助手 v0.1 toc: true comments: true date: 2016-06-04 16:40:06 tags: [Python, Markdown ...
- [C#] 简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Wen. ...
- 工大助手(C#与python交互)
工大助手(爬虫--C#与python交互) 基本内容 工大助手(桌面版) 实现登陆.查成绩.计算加权平均分等功能 团队人员 13070046 孙宇辰 13070003 张帆 13070004 崔巍 1 ...
- EnumHelper.cs枚举助手(枚举描述信息多语言支持)C#
C#里面经常会用到枚举类型,枚举是值类型对象,如果你想用枚举类型的多属性特性,或者你想在MVC页面上通过简单的值类型转换,将某字段值所代表的含义转换为文字显示,这时候必须要将枚举扩展,是它支持文本描述 ...
- C#远程时间同步助手软件设计
C#远程时间同步助手软件设计 本程序才C#语言开发,实现远程时间同步功能,可以将本地时间每隔一段时间与时间服务器时间进行同步!不足之处还望见谅! 软件开发环境:Visual Studio 2010 软 ...
- 活动助手Beta用户试用报告
用户试用报告 1.面向参与者用户 1.1 日常参加各类学习(水综测)活动中,有没有遇到以下问题: (1) 信息来源混乱,不知道靠不靠谱 (2) 每次报名都要重新填写自己的学号手机号,有时候填错了就没综 ...
- ThinkPHP5 助手函数
对于ThinkPHP5.0以前的版本,助手函数全部是单字母函数,但到ThinkPHP5之后,使用如下函数来代替单字母函数: 最常用: /** * 实例化Model * @param string $n ...
- 谢欣伦 - OpenDev原创例程 - 串口助手Comm Assist
前一段时间,一位博友发邮件给我.他跟我讲说没太看懂<化繁为简系列原创教程 - 通信专题 - 串口类CxComm的使用>,请我做一个DEMO工程给他.我抽了一天时间编写并上传了一个DEMO工 ...
- 微信小程序之ES6与事项助手
由于官方IDE更新到了0.11.112301版本,移除了对Promise的支持,造成事项助手不能正常运行,解决此问题,在项目中引入第三方兼容库Bluebird支持Promise,代码已经整合到项目代码 ...
- 12306订票助手.net版如何抢指定过路某一地点的火车票
12306订票助手.net版如何抢指定路过某一地点的火车票? 直接举例: 广州到武汉,很多高铁,经过清远,衡阳,郴州,长沙等地.需要从清远上车.操作步骤如下: 1.先查询清远-武汉,打开右下角的自动预 ...
随机推荐
- ext2文件系统学习(二)—— 目录磁盘结构
创建镜像.mount等操作和上一篇一样,测试目录结构如下: 一些文件系统信息如下: Block size: 1024 Inodes per group: 128 Inode ...
- Linux命令Find实例
转自: http://www.tecmint.com/35-practical-examples-of-linux-find-command/ 35 Practical Examples of Lin ...
- 使用 Docker 搭建 Tomcat 运行环境
转自: http://m.oschina.net/blog/616526, 版权归原作者所有. 1 Docker与虚拟机 2 搭建过程 2.1 准备宿主系统 准备一个 CentOS 7操作系统 ...
- android:碎片的概念
碎片(Fragment)是一种可以嵌入在活动当中的 UI 片段,它能让程序更加合理和充分 地利用大屏幕的空间,因而在平板上应用的非常广泛.虽然碎片对你来说应该是个全新的概 念,但我相信你学习起来应该毫 ...
- .NET:Threading and Exceptions
Do handle exceptions in threads. Unhandled exceptions in threads, even background threads, generally ...
- mysql 时间类型精确到毫秒、微秒及其处理
一.MySQL 获得毫秒.微秒及对毫秒.微秒的处理 MySQL 较新的版本中(MySQL 6.0.5),也还没有产生微秒的函数,now() 只能精确到秒. MySQL 中也没有存储带有毫秒.微秒的日期 ...
- Redis源码学习-Master&Slave的命令交互
0. 写在前面 Version Redis2.2.2 Redis中可以支持主从结构,本文主要从master和slave的心跳机制出发(PING),分析redis的命令行交互. 在Redis中,serv ...
- Spring MVC报异常:org.springframework.web.util.NestedServletException: Request processing failed
在使用SpringMVC绑定基本类型(如String,Integer等)参数时,应通过@RequestParam注解指定具体的参数名称,否则,当源代码在非debug模式下编译后,运行时会引发Handl ...
- windows IOCP入门的一些资料
最近需要看一个中间件的IOCP处理模块,需要了解这方面的知识 https://www.codeproject.com/articles/13382/a-simple-application-using ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...