根據(jù)json字符串生成Html的一種方式
本文介紹了根據(jù)Json串生成Html的一種方式(只是簡(jiǎn)單實(shí)現(xiàn)了文本框,密碼框,下拉框)。只是覺(jué)得好玩才這樣做,如果覺(jué)得沒(méi)有任何價(jià)值,請(qǐng)忽略。不足指出希望各位大牛指點(diǎn)。后續(xù)將根據(jù)各位的指點(diǎn)繼續(xù)完善。
功能說(shuō)明:

在左側(cè)輸入框中輸入Json串,點(diǎn)擊執(zhí)行時(shí)根據(jù)輸入的Json串在右側(cè)展示區(qū)顯示出相應(yīng)的Html(使用Jquery1.4.4)
HTML:
<table style="width:100%; ">
<col width="200px;" />
<tr>
<td>Json輸入框</td>
<td>展示區(qū)</td>
</tr>
<tr>
<td>
<textarea id="txtJson" rows="20" cols="50">
</textarea>
</td>
<td valign="top">
<div id="divShow">
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<input id="btnExec" type="button" value="執(zhí)行" />
</td>
</tr>
</table>
JS代碼:
$(document).ready(function () {
$("#btnExec").click(function () {
try{
var objList = eval($("#txtJson").val());
jsonToControl(objList);
}
catch(e){
alert("json格式錯(cuò)誤");
}
});
});
function jsonToControl(jsonObj) {
$("#divShow").empty();
$.each(jsonObj, function (index, item) {
var control = null;
var title = $("<label />");
switch (item.type) {
case "textbox":
control = createTextBox();
break;
case "select":
control = createSelect(item);
break;
case "password":
control = createPassword();
break;
//------------------------------
// 其它控件在這里加代碼
//------------------------------
}
if (item.title != null) {
title.text(item.title);
}
if (control != null) {
control = setAttritube(control, item);
$("#divShow").append(title);
$("#divShow").append(control);
$("#divShow").append("<br/>");
}
})
}
//設(shè)置控件的樣式
function setAttritube(control, item) {
if (item.width != null) {
control.width(item.width);
}
//--------------------------------
// 其他樣式在這里加代碼
//--------------------------------
return control;
}
//創(chuàng)建TextBox
function createTextBox() {
return $("<input type='textbox' />");
}
//創(chuàng)建密碼框
function createPassword() {
return $("<input type='password'/>");
}
//創(chuàng)建Select
function createSelect(item) {
var c = $("<select></select>");
if(item.items != null ){
$.each(item.items,function(index,i){
$("<option value='"+i.key+"' checked='checked'>"+i.value+"</option>").appendTo(c);
})
}
return c;
}
非常感謝各位抽空看完。如果有任何意見(jiàn)或建議,請(qǐng)留言。
相關(guān)文章
JavaScript setTimeout與setTimeinterval使用案例詳解
這篇文章主要介紹了JavaScript setTimeout與setTimeinterval使用案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Next.js使用getServerSideProps進(jìn)行服務(wù)器端渲染demo
這篇文章主要為大家介紹了Next.js使用getServerSideProps進(jìn)行服務(wù)器端渲染demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Fastest way to build an HTML string(拼裝html字符串的最快方法)
Fastest way to build an HTML stringPosted in 'Code Snippets, JavaScript' by James on May 29th, 20092011-08-08微信小程序?qū)崿F(xiàn)列表滾動(dòng)頭部吸頂?shù)氖纠a
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)列表滾動(dòng)頭部吸頂?shù)氖纠a,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07JavaScript 井字棋人工智能實(shí)現(xiàn)代碼
JavaScript fights back in this artificial Tic Tac Toe game. Great script to have to entertain yourself and your visitors.2009-12-12簡(jiǎn)單實(shí)現(xiàn)JS倒計(jì)時(shí)效果
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)JS倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12JavaScript 筆記二 Array和Date對(duì)象方法
本篇主要講解本地對(duì)象Array和Date的各種方法。2010-05-05moment.js輕松實(shí)現(xiàn)獲取當(dāng)前日期是當(dāng)年的第幾周
這篇文章主要介紹了moment.js輕松實(shí)現(xiàn)獲取當(dāng)前日期是當(dāng)年的第幾周,需要的朋友可以參考下2015-02-02