2012-12-24 41 views
1

是否有可能使用JavaScript將所有div子信息轉換爲XML或JSON?html to xml轉換使用javascript?

$("#droppable").droppable({ 
drop : function(event, ui) { 
    var id = $(ui.draggable).attr("id"); 
    var cloneObj = $((ui.draggable).clone()); 
    $(cloneObj).removeClass("draggable ui-draggable"); 
    if (id === "txt") { 
     inputOBj = document.createElement("input"); 
     inputOBj.setAttribute("id", "txt" + i); 
     $("#droppable").append(inputOBj); 
    } else if (id == "combo") { 
     inputOBj = document.createElement("select"); 
     inputOBj.setAttribute("id", "select" + i); 
     console.log(""); 
    } 
    }); 
+0

從我所瞭解的html和xml密切相關。你想從另一個轉換爲一個特定的原因? – Neeraj

+0

感謝您的回覆。 我想用javascrip中的Drag -n-Drop生成html代碼。 我能夠在DnD中創建hmtl表單,但要保存到單獨的文件 我需要xml/json或任何可以用來將生成的代碼保存到單獨文件中的任何文件。 –

+0

這個答案可能會幫助 http://stackoverflow.com/questions/10866997/js-jquery-other-library-plugin-to-convert-html-xml – Neeraj

回答

1

有屬性叫做outerHTML。 它以HTML格式設置或檢索對象及其內容。你可以按以下方式使用它。 e.g:

$(document).ready(function() { 
    $('#p').click(function() { 
     alert($('#p')[0].outerHTML); 
    }); 
}); 

提示:p是您在網頁正文中的任何標籤ID。

+0

好的答案...它也工作。但以上2個答案有什麼區別? –

4

我相信你可以使用XMLSerializer來做到這一點。

var yourString = new XMLSerializer().serializeToString(cloneObj[0]); 
+0

分號。 優秀的答案... :) 謝謝 –