js⽤CreateElement动态创建标签⽰例//定义⽅法创建⼀个label标签
//*************************************//
复制代码代码如下:
var createLabel = function(id, name, value) {
var label_var = ateElement("label");
var label_id = ateAttribute("id");
deValue = id;
var label_text = ateTextNode(value);
label_var.setAttributeNode(label_id);
var label_css = ateAttribute("class");
deValue = "select_css";
label_var.setAttributeNode(label_css);
label_var.appendChild(label_text);
return label_var;
}
//*************************************//
//定义⽅法创建input标签(主要为Text)
//id,name,value,type 分别代表创建标签的id,
// 名称(name),值(value),类型(type)
// 绑定Input⽅法事件,绑定⽅式如下(可以同时绑定多个事件⽅法):
// "onchange==alert('This Value is change success !');|onblur==alert('This value is the beautiful one !');" //*************************************//
复制代码代码如下:
var createInput = function(id, name, value, type, width, height, event) {
var var_input = null;
var input_event_attr_IE = "";
if (event != null && event != "") {
var event_array_IE = String().split('|');
for (var i = 0; i < event_array_IE.length; i++) {
var event_IE = event_array_IE[i].split('==');
input_event_attr_IE += " " + event_IE[0] + "='' ";
}
}
try {//定义变量实现IE6.0和IE7.0兼容。
var_input = ateElement("<input " + input_event_attr_IE + ">");
} catch (e) {
var_input = ateElement("input");
}
var input_id = ateAttribute("id");
deValue = id;
var input_name = ateAttribute("name");
input绑定onblur事件deValue = name;
var input_type = ateAttribute("type");
deValue = type;
var input_value = ateAttribute("value");
deValue = value;
var input_style = ateAttribute("style");
var input_style_str = "";
if (width != null && width != "") {
input_style_str += "width:" + width + "px;";
} else {
input_style_str += "width:30px;";
}
if (height != null && height != "") {
input_style_str += "height:" + height + "px;";
}
if (event != null && event != "") {
var event_array = String().split('|');
for (var i = 0; i < event_array.length; i++) {
var events = event_array[i].split('==');
var input_event = ateAttribute(events[0]);
deValue = events[1];
var_input.setAttributeNode(input_event);
}
}
var_input.setAttributeNode(input_type);
deValue = input_style_str;
try {
var_input.setAttributeNode(input_style);
} catch (e) {
width = (width == null || width == "") ? "30" : width;
var_input.setAttribute("width", width);
if (height != null && height != "") {
var_input.setAttribute("height", height);
}
}
// if (readonly != "") {
// var input_readonly = ateAttribute("readonly");
/
/ deValue = "readonly";
// var_input.setAttributeNode(input_readonly);
// }
var_input.setAttributeNode(input_id);
var_input.setAttributeNode(input_name);
var_input.setAttributeNode(input_value);
return var_input;
}
//******************************************************************//
//定义⽅法创建⼀个Select选择框的标签;
//***** id 表⽰标签的标识id
/
/***** name 表⽰标签的名称name
//***** options表⽰标签要绑定的选择项(例如:"0231A563-专业类服务|02312177-维保类服务|……") //***** splitstr表⽰⽤来分割options的字符(如:'|')
//***** splitchar表⽰分割键值对的分隔符(如:'-')
//***** event 表⽰此标签对应的事件(当event==null时此标签不绑定事件)
//******************************************************************//
复制代码代码如下:
var createSelect = function(id, name, options, splitstr, splitchar, event, selectedValue) {
var var_select = null;
try {//处理IE6.0和IE7.0的兼容问题。
var_select = ateElement("<select onchange='' >");
} catch (e) {
var_select = ateElement("select");
}
var select_id = ateAttribute("id");
deValue = id;
var select_name = ateAttribute("name");
deValue = name;
if (event != null && event != undefined && event != "") {
var select_change = ateAttribute("onchange");
deValue = event;
var_select.setAttributeNode(select_change);
}
var_select.setAttributeNode(select_id);
var_select.setAttributeNode(select_name);
try {
var_select.setAttribute("width", "100px");
} catch (e) {
var select_css = ateAttribute("class");
deValue = "select_css";
var_select.setAttributeNode(select_css);
}
splitstr = (splitstr == "" || splitstr == null) ? "|" : splitstr;
splitchar = (splitchar == "" || splitchar == null) ? "-" : splitchar;
if (options != null && options != undefined && String() != "") {
options = (String().lastIndexOf(splitstr) + 1 == String().length) ? String().substr(0,
var arrayOption = String().split(splitstr);
for (var i = 0; i < arrayOption.length; i++) {
var temp_value = arrayOption[i].split(splitchar);
var option = ateElement("option");
var option_value = ateAttribute("value");
deValue = temp_value[0];
var option_text = ateTextNode(temp_value[1]);
option.setAttributeNode(option_value);
option.appendChild(option_text);
var_select.appendChild(option);
if (selectedValue != null && selectedValue != "") {
if (temp_value[0] == selectedValue || temp_value[1] == selectedValue) {
var_select.options[i].selected = true;
}
}
}
}
return var_select;
}
/
/***************************************************//
//定义⽅法创建⼀个<a>标签;
//***** id表⽰标签唯⼀表⽰id
//***** name表⽰标签的名称name
//***** value表⽰标签对应显⽰的⽂字(名称)
//***** event表⽰标签对应的事件(当event==null时事件不绑定)
//***** href表⽰标签的链接属性
//***************************************************//
复制代码代码如下:
var createA = function(id, name, value, event, href, target) {
var var_a = null;
try {
var_a = ateElement("<a onclick='' target='_blank'>"); //这⾥创建必须为"<a onclick='alert()'>"这种形式来创建否者不⽀持IE6.0和IE7.0
} catch (e) {
var_a = ateElement("a");
}
var a_id = ateAttribute("id");
deValue = id;
var a_name = ateAttribute("name");
deValue = name;
href = (href == null || href == "") ? ("javascript:void(0);" || "#") : href;
var a_href = ateAttribute("href");
deValue = href;
var a_Text = ateTextNode(value);
var_a.setAttributeNode(a_href);
var_a.setAttributeNode(a_id);
var_a.setAttributeNode(a_name);
if (target != null) {
var target_href = ateAttribute("target");
deValue = "_blank";
var_a.setAttributeNode(target_href);
}
if (event != "" && event != null && event != undefined) {
var a_click = ateAttribute("onclick");
deValue = event;
var_a.setAttributeNode(a_click);
}
var_a.appendChild(a_Text); //注意这个值绑定顺序,只能放在最后去绑定(不然不⽀持IE6.0和IE7.0)return var_a;
}
//******************************************//
//定义⽅法判断输⼊值是否为数字;
//******* 当flag=true时判断输⼊值是否为整数;
//******************************************//
复制代码代码如下:
var check_Is_Num = function(obj, flag) {
var flag_var = false;
var num = /^\d+$/; ///^\+?[1-9][0-9]*$/;
//flag_var = /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(obj);
flag_var = /^\d+(\.\d+)?$/.test(obj);
if (flag) {
flag_var = st(obj);
}
return flag_var;
}
//定义⽅法删除节点。
var removeRowItem = function(obj) {
var rowTr = obj.parentNode.parentNode;
try {
} catch (e) {
veChild(rowTr);
}
}
String.prototype.Trim = function() {
place(/(^\s*)|(\s*$)/g, "");
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论