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.先查询清远-武汉,打开右下角的自动预 ...
随机推荐
- Spring 3.1 M1: Unified Property Management(转)
In the first two posts of this series, I described the bean definition profiles feature, and how it ...
- [CGAL]带岛多边形三角化
CGAL带岛多边形三角化,并输出(*.ply)格式的模型 模型输出的关键是节点和索引 #include <CGAL/Triangulation_vertex_base_with_id_2.h&g ...
- ThreadLocal 简介 案例 源码分析 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Android典型界面设计(8) ——ViewPager+PagerSlidingTabStrip实现双导航
一.问题描述 PagerSlidingTabStrip是android开源项目,指示器控件.官网地址:https://github.com/astuetz/PagerSlidingTabStrip 该 ...
- 使用log4net将C#日志发送到Elasticsearch
一.安装Elasticsearch 参考前面写的文章:https://www.cnblogs.com/songxingzhu/p/7909486.html 安装完Elasticsearch后,修改/e ...
- redis 频率限制
方式1: $redis = new Redis(); //以自然时间控制 一自然分钟内超过100次进行限制, 屏蔽多久的时间必须为计数key时间的倍数 $key = 'xxxx'.date('Y-m- ...
- sublime 3插件推荐
新建文件以及快速注释 1. SublimeTmpl 快速生成文件模板 一直都很奇怪为什么sublime text 3没有新建文件模板的功能,像html头部的DTD声明每次都要复制粘贴.用Subli ...
- maven打包含有多个main程序的jar包及运行方式
pom.xml <build> <plugins> <plugin> <groupId> ...
- IDEA使用笔记(六)——设置项目的JDK配置
1:由于dev分支和master分支的代码差异比较多,所以,就从master上分出一个新的分支dev_,于是我就克隆新的代码,打开对应的项目文件,然后启动试试,发现报出如下的错误,很明显是因为没有制定 ...
- OpenCV 学习笔记 05 级联分类器CascadeClassifier类
在人脸检测中,CascadeClassifier 是一个类,该类的作用是(基于官方已经训练好的数据文件 .xml)实例化一个检测器. 1 类 CascadeClassifier 的概述 首先看一下该类 ...