Fre*_*red 5 html javascript jquery json
我正在使用jQuery模板插件从JSON数据生成HTML,用户操作(并且可能会改变).我正在寻找一种方法来将此html读回JSON,以便将其保存回我的服务器.jQuery提供了一个$ .tmplItem()方法,它返回最初设置的dataJSON,但我想知道如何获取当前DOM中的值?
这个怎么样?
拉出您所在区域的HTML,然后将其转换回JSON:
$(':button').click(function() {
$('#output').text(JSON.stringify({
data:$('#input').html()
}));
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="input" contenteditable="true" style="border: 1px solid;width:300px; height:100px;"><b>Edit me!</b> You can use CTRL+B to change bold, ctrl+I to change italics.</div>
<div style="clear:both;">
<input type="button" value="Get HTML">
</div>
<div id="output" style="border: 1px solid;clear:both;width:300px;height:100px;font-family:courier;font-size:10px;">
</div>Run Code Online (Sandbox Code Playgroud)
顺便说JSON.stringify一下,我用的是简单的,但是在生产环境中,你应该使用像jquery-json这样的库,因为一些不支持它的旧浏览器仍然在徘徊.
| 归档时间: |
|
| 查看次数: |
28481 次 |
| 最近记录: |