根据json字符串生成Html的一种方式_javascript技巧

JS代码: 复制代码 代码如下: $.ready { $.click { try{ var objList = eval; jsonToControl{ alert; function jsonToControl { $; $.each(jsonObj, function { var control = null; var title = $; switch { case "textbox": control = createTextBox(); break; case "select": control = createSelect; break; case "password": control = createPassword(); break; //------------------------------ // 其余控件在这间加代码 //------------------------------ } if { title.text; } if { control = setAttritube; $.append.append; $.append } //设置控件的体制 function setAttritube { if { control.width; } //-------------------------------- // 其余样式在这里边加代码 //-------------------------------- return control; } //创设Text博克斯function createTextBox() { return $(""); } //创制密码框 function createPassword() { return $(""); } //创制Select function createSelect { var c = $; if{ $.each(item.items,function{ $(""+i.value+"").appendTo } return c; } 特别多谢各位抽空看完。即使有别的意见或建议,请留言。

复制代码 代码如下: /* DisplayModule interface. */ var DisplayModule = new Interface('DisplayModule', ['append', 'remove', 'clear']); /* ListDisplay class. */ //通过接口实现工厂,那是通过List情势突显KugaSS var ListDisplay = function { // implements DisplayModule this.list = document.createElement; this.list.id = id; parent.appendChild; }; ListDisplay.prototype = { append: function { var newEl = document.createElement; this.list.appendChild; newEl.innerHTML = text; return newEl; }, remove: function { this.list.removeChild; }, clear: function() { this.list.innerHTML = ''; } }; /* Configuration object. */ var conf = { id: 'cnn-top-stories', feedUrl: '', updateInterval: 60, // In seconds. parent: $ }; /* FeedReader class. */ var FeedReader = function(display, xhrHandler, conf) { this.display = display; this.xhrHandler = xhrHandler; this.conf = conf; this.startUpdates(); }; FeedReader.prototype = { fetchFeed: function() { var that = this; var callback = { success: function { that.parseFeed; }, failure: function { that.showError; } }; this.xhrHandler.request('GET', 'feedProxy.php?feed=' + this.conf.feedUrl, callback); }, parseFeed: function(responseText, responseXML) { this.display.clear(); var items = responseXML.getElementsByTagName; for(var i = 0, len = items.length; i < len; i++) { var title = items[i].getElementsByTagName[0]; var link = items[i].getElementsByTagName[0]; this.display.append('' + title.firstChild.data + ''); } }, showError: function { this.display.clear(); this.display.append('Error fetching feed.'); }, stopUpdates: function() { clearInterval; }, startUpdates: function; var that = this; this.interval = setInterval { that.fetchFeed(); }, this.conf.updateInterval * 1000); } }; /* FeedManager namespace. */ //工厂微电脑,这里能够依靠传进来的参数选取具体的Display var FeedManager = { createFeedReader: function { var displayModule = new ListDisplay(conf.id + '-display', conf.parent); Interface.ensureImplements(displayModule, DisplayModule); var xhrHandler = XhrManager.createXhrHandler(); Interface.ensureImplements(xhrHandler, AjaxHandler); return new FeedReader(displayModule, xhrHandler, conf); } }; ===================================================== 另八个足踏车工厂的例证: var BicycleShop = function() {}; BicycleShop.prototype = { sellBicycle: function { var bicycle = this.createBicycle; bicycle.assemble; return bicycle; }, createBicycle: function { throw new Error('Unsupported operation on an abstract class.'); } }; /* AcmeBicycleShop class. */ var AcmeBicycleShop = function() {}; extend(AcmeBicycleShop, BicycleShop); AcmeBicycleShop.prototype.createBicycle = function { var bicycle; switch { case 'The Speedster': bicycle = new AcmeSpeedster(); break; case 'The Lowrider': bicycle = new AcmeLowrider(); break; case 'The Flatlander': bicycle = new AcmeFlatlander(); break; case 'The Comfort Cruiser': default: bicycle = new AcmeComfortCruiser(); } Interface.ensureImplements; return bicycle; }; /* GeneralProductsBicycleShop class. */ var GeneralProductsBicycleShop = function() {}; extend(GeneralProductsBicycleShop, BicycleShop); GeneralProductsBicycleShop.prototype.createBicycle = function { var bicycle; switch { case 'The Speedster': bicycle = new GeneralProductsSpeedster(); break; case 'The Lowrider': bicycle = new GeneralProductsLowrider(); break; case 'The Flatlander': bicycle = new GeneralProductsFlatlander(); break; case 'The Comfort Cruiser': default: bicycle = new GeneralProductsComfortCruiser(); } Interface.ensureImplements; return bicycle; }; /* Usage. */ 复制代码 代码如下: var alecsCruisers = new AcmeBicycleShop(); var yourNewBike = alecsCruisers.sellBicycle; var bobsCruisers = new GeneralProductsBicycleShop(); var yourSecondNewBike = bobsCruisers.sellBicycle;

  • msg + "

小说证实:本文介绍了基于Json串生成Html的黄金时代种方法。只是以为有意思才那样做,要是以为未有其它价值,请忽略。不足提议希望各位大腕辅导。后续将依据各位的辅导继续周密。 意义表达: 在左臂输入框中输入Json串,点击实行时依据输入的Json串在左侧呈现区呈现出相应的Html HTML: 复制代码 代码如下:

复制代码 代码如下: var _alert_iconCss = "tipmsg_icoInfo"; var _confirm_iconCss = "tipmsg_icoConfirm"; var _error_iconCss = "tipmsg_icoError"; var _warning_iconCss = "tipmsg_icoWarning"; function dialogInit { var iconCss = ""; switch { case "confirm" : iconCss = _confirm_iconCss; break; case "error" : iconCss = _error_iconCss; break; case "warning" : iconCss = _warning_iconCss; break; default : iconCss = _alert_iconCss; break; } var htmlStr = "

本文由必威发布于必威-运维,转载请注明出处:根据json字符串生成Html的一种方式_javascript技巧

相关阅读