js 用CreateElement动态创建标签示例

//定义方法创建一个label标签
//*************************************//

复制代码 代码如下:


var createLabel = function(id, name, value) {
var label_var = document.createElement("label");

var label_id = document.createAttribute("id");
label_id.nodeValue = id;

var label_text = document.createTextNode(value);

label_var.setAttributeNode(label_id);
var label_css = document.createAttribute("class");
label_css.nodeValue = "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=https://www.jb51.net/article/=alert('This Value is change success !');|onblur=https://www.jb51.net/article/=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 = event.toString().split('|');
for (var i = 0; i < event_array_IE.length; i++) {
var event_IE = event_array_IE[i].split('=https://www.jb51.net/article/=');
input_event_attr_IE += " " + event_IE[0] + "='' ";
}
}
try {//定义变量实现IE6.0和IE7.0兼容。
var_input = document.createElement("<input " + input_event_attr_IE + ">");
} catch (e) {
var_input = document.createElement("input");
}

var input_id = document.createAttribute("id");
input_id.nodeValue = id;
var input_name = document.createAttribute("name");
input_name.nodeValue = name;
var input_type = document.createAttribute("type");
input_type.nodeValue = type;
var input_value = document.createAttribute("value");
input_value.nodeValue = value;
var input_style = document.createAttribute("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 = event.toString().split('|');
for (var i = 0; i < event_array.length; i++) {
var events = event_array[i].split('=https://www.jb51.net/article/=');
var input_event = document.createAttribute(events[0]);
input_event.nodeValue = events[1];
var_input.setAttributeNode(input_event);
}
}

var_input.setAttributeNode(input_type);
input_style.nodeValue = input_style_str;
try {
var_input.setAttributeNode(input_style);
} catch (e) {
width = (width =https://www.jb51.net/article/= null || width =https://www.jb51.net/article/= "") ? "30" : width;
var_input.setAttribute("width", width);
if (height != null && height != "") {
var_input.setAttribute("height", height);
}
}
// if (readonly != "") {
// var input_readonly = document.createAttribute("readonly");
// input_readonly.nodeValue = "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=https://www.jb51.net/article/=null时此标签不绑定事件)
//******************************************************************//

复制代码 代码如下:


var createSelect = function(id, name, options, splitstr, splitchar, event, selectedValue) {
var var_select = null;
try {//处理IE6.0和IE7.0的兼容问题。
var_select = document.createElement("<select onchange='' >");
} catch (e) {
var_select = document.createElement("select");
}

var select_id = document.createAttribute("id");
select_id.nodeValue = id;
var select_name = document.createAttribute("name");
select_name.nodeValue = name;

if (event != null && event != undefined && event != "") {
var select_change = document.createAttribute("onchange");
select_change.nodeValue = 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 = document.createAttribute("class");
select_css.nodeValue = "select_css";
var_select.setAttributeNode(select_css);
}

splitstr = (splitstr =https://www.jb51.net/article/= "" || splitstr =https://www.jb51.net/article/= null) ? "|" : splitstr;
splitchar = (splitchar =https://www.jb51.net/article/= "" || splitchar =https://www.jb51.net/article/= null) ? "-" : splitchar;

if (options != null && options != undefined && options.toString() != "") {
options = (options.toString().lastIndexOf(splitstr) + 1 =https://www.jb51.net/article/= options.toString().length) ? options.toString().substr(0, options.toString().length - 1) : options;
var arrayOption = options.toString().split(splitstr);
for (var i = 0; i < arrayOption.length; i++) {
var temp_value = arrayOption[i].split(splitchar);
var option = document.createElement("option");
var option_value = document.createAttribute("value");
option_value.nodeValue = temp_value[0];
var option_text = document.createTextNode(temp_value[1]);
option.setAttributeNode(option_value);
option.appendChild(option_text);

var_select.appendChild(option);
if (selectedValue != null && selectedValue != "") {
if (temp_value[0] =https://www.jb51.net/article/= selectedValue || temp_value[1] =https://www.jb51.net/article/= selectedValue) {
var_select.options[i].selected = true;
}
}
}
}
return var_select;
}


//***************************************************//
//定义方法创建一个<a>标签;
//***** id表示标签唯一表示id
//***** name表示标签的名称name
//***** value表示标签对应显示的文字(名称)
//***** event表示标签对应的事件(当event=https://www.jb51.net/article/=null时事件不绑定)
//***** href表示标签的链接属性
//***************************************************//

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdwsgj.html