我正在嘗試使用DOM動態創建HTML元素。DOM正在創建無序的HTML元素
這些元素都是繪製的,只是它們看起來不合理。
例如這裏是它創建並添加元素的功能是這樣的:
function inputForms(goal){
if(goal=="fatloss"){
// remove any form previous created
$("#"+"input_child").remove();
// create new form section
createDiv("input_parent","input_child");
// create form elements
Label("input_parent","curr_act_label","curr_act_lvl","Current Activity Level:");
selectInputElement("input_child","curr_act_lvl");
selectOption("curr_act_lvl","Please Select Your Current Activity Level...", "none");
selectOption("curr_act_lvl","No Exercise or Desk Job", "1.25");
selectOption("curr_act_lvl","Little Daily Activity or Light Exercise 1-3 Days Weekly", "1.35");
selectOption("curr_act_lvl","Moderately Active Lifestyle or Moderate Exercise 3-5 Days Weekly", "1.55");
selectOption("curr_act_lvl","Physically Demanding Lifestyle or Hard Exercise 6-7 Days Weekly", "1.75");
selectOption("curr_act_lvl","Hardcore Lifestyle 24/7", "1.95");
Label("input_parent","curr_bodyfat_label","curr_bodyfat_input","Current Bodyfat:");
InputField("input_parent","curr_bodyfat_input","text",5,3)
Label("input_parent","curr_weight_label","curr_weight_input","Current Weight:");
InputField("input_parent","curr_weight_input","text",5,3)
Label("input_parent","meals_label","meals_input","Daily Meals:");
InputField("input_parent","meals_input","text",5,1)
Label("input_parent","goal_bf_pct_label","curr_weight_input","Current Weight:");
InputField("input_parent","curr_weight_input","text",5,3)
Label("input_parent","time_label","time_input","Weeks To Goal:");
InputField("input_parent","time_input","text",5,2)
Label("input_parent","deficit_label","deficit_select","Decifict By:");
selectInputElement("input_child","deficit_select");
selectOption("deficit_select","Please Select a deficit Method:", "none");
selectOption("deficit_select","Burn With Exercise", "burn");
selectOption("deficit_select","Fasting/Calorie Restriction Without Exercise", "starve");
結果是,儘管他們之前已經創建的標籤元素的兩個「選擇」的元素在最高層出現。
所以這最終渲染這樣的:
選擇元素
選擇元素
標籤< ---屬於第一選擇要素
標籤
inputfield
標籤
輸入字段
標籤
輸入字段
標籤
標籤< - 屬於第二輸入字段
它應該看起來更像這個(我加了空格來強調組織):
標籤 選擇元素
標籤 inputfield
標籤 輸入字段
標籤 輸入字段
標籤 選擇元素
這裏是我的函數來創建這些元素:
function createDiv(section_id, div_id){
section=document.getElementById(section_id);
div=document.createElement("div");
div.setAttribute("id",div_id);
section.appendChild(div);
}
function selectInputElement(section_id,input_select_id){
section=document.getElementById(section_id);
select_element=document.createElement("select");
select_element.setAttribute("id",input_select_id);
section.appendChild(select_element);
}
function selectOption(input_select_id,option_text,option_value){
element=document.getElementById(input_select_id);
option=document.createElement("OPTION");
text=document.createTextNode(option_text);
option.setAttribute("value",option_value);
option.appendChild(text);
element.appendChild(option);
}
function InputField(section_id,input_id,element_type,size,length){
section=document.getElementById(section_id);
input_field=document.createElement("input");
input_field.setAttribute("id",input_id);
input_field.setAttribute("type",element_type);
input_field.setAttribute("maxlength",size);
input_field.setAttribute("size",length);
section.appendChild(input_field);
}
function Label(section_id,label_id,label_for,label_text){
section=document.getElementById(section_id);
label=document.createElement("label");
label.setAttribute("id",label_id);
label.setAttribute("for",label_for);
label_txt=document.createTextNode(label_text);
label.appendChild(label_txt);
section.appendChild(label);
}
我不認爲我們可以說很多,直到我們知道'Label'和'selectInputElement'(以及其他函數)如何工作。 – 2011-12-31 20:11:12
「我之前創建過這個」現在在哪裏? – emaillenin 2011-12-31 20:11:54
我編輯了原來的問題菲利克斯。 emaillenin - 我不知道如何「刷新」已存在的元素,因爲每次我調用這些函數時,元素都會堆疊在一起。所以我改變了我正在做的'隱藏',並使可見的元素塊。後來我發現我可以使用$(「#」+「input_child」)來'刷新'創建的元素。刪除已創建的內容並重新繪製我想要的元素的正確組合。 – 2011-12-31 20:17:09