- ferreousbox
- 等级:
- 性别:
- 文章: 77
- 积分: 150
- 来自: 杭州
|
再来第二个使用类,select元素的操作使用类。针对网页中select元素的操作实用类(静态方法)
js 代码
-
-
-
-
- function Select(){};
-
-
-
- Select.create = function(selectId,json) {
- Select.clear(selectId);
- Select.add(selectId, json);
- };
-
-
-
- Select.add = function(selectId,json) {
- try {
- if (!json.options) return;
- for (var i = 0; i < json.options.length; i ++) {
- Select.addOption(selectId,json.options[i].value,json.options[i].text);
- }
- } catch (ex) {
- alert('设置select错误:指定的JSON对象不符合Select对象的解析要求!');
- }
- };
-
-
-
- Select.createOption = function(value, text) {
- var opt = document.createElement('option');
- opt.setAttribute('value', value);
- opt.innerHTML = text;
- return opt;
- };
-
-
-
- Select.addOption = function(selectId, value, text) {
- var opt = Select.createOption(value, text);
- $(selectId).appendChild(opt);
- return opt;
- };
-
-
-
- Select.getSelected = function(selectId) {
- var slt = $(selectId);
- if (!slt) return null;
- if (slt.type.toLowerCase() == "select-multiple") {
- var len = Select.len(selectId);
- var result = [];
- for (var i = 0; i < len; i ++) {
- if (slt.options[i].selected) result.push(slt.options[i]);
- }
- return result.length > 1 ? result : (result.length == 0 ? null : result[0]);
- } else {
- var index = $(selectId).selectedIndex;
- return $(selectId).options[index];
- }
- };
-
-
-
- Select.select = function(selectId, index) {
- var slt = $(selectId);
- if (!slt) return false;
- for (var i = 0; i < slt.options.length; i ++) {
- if (index == i) {
- slt.options[i].setAttribute("selected", "selected");
- return true;
- }
- }
- return false;
- };
-
-
-
- Select.selectAll = function(selectId) {
- var len = Select.len(selectId);
- for (var i = 0; i < len; i ++) Select.select(selectId, i);
- };
-
-
-
- Select.len = function(selectId) {
- return $(selectId).options.length;
- };
-
-
-
- Select.clear = function(selectId, iterator) {
- if (typeof(iterator) != 'function') {
- $(selectId).length = 0;
- } else {
- var slt = $(selectId);
- for (var i = slt.options.length - 1; i >= 0; i --) {
- if (iterator(slt.options[i]) == true) slt.removeChild(slt.options[i]);
- }
- }
- };
-
-
-
-
-
- Select.copy = function(srcSlt, targetSlt, iterator) {
- var s = $(srcSlt), t = $(targetSlt);
- for (var i = 0; i < s.options.length; i ++) {
- if (typeof(iterator) == 'function') {
- if (iterator(s.options[i], $(targetSlt).options) == true) {
- t.appendChild(s.options[i].cloneNode(true));
- }
- } else {
- t.appendChild(s.options[i].cloneNode(true));
- }
- }
- };
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|