jQuery HTML to JSON

前端 未结 2 1230
南笙
南笙 2020-12-17 03:51

I\'m using the jQuery template plugin to generate HTML from JSON data which the user than manipulates (and, potentially alters). I\'m looking for a way to read this html bac

相关标签:
2条回答
  • 2020-12-17 03:55

    The nicest way to get this done will be some sort of data binding, such as the jQuery data link plugin or (perhaps a bit much for your current needs) Knockout.

    0 讨论(0)
  • 2020-12-17 04:13

    How about this?

    http://jsfiddle.net/mWuHe/14/

    Pull out the HTML of your area, then convert it back to JSON:

    $(':button').click(function() {
        $('#output').text(JSON.stringify({ 
            data:$('#input').html() 
        }));
    });
    <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>

    btw I used JSON.stringify for simplicty, but in a production environment you should probably use a library like jquery-json, since some old browsers that don't support that are still skulking around.

    0 讨论(0)
提交回复
热议问题