简体   繁体   English

Javascript源到textarea值

[英]Javascript Source to textarea value

I am creating some JSON on the fly, serializing it and saving it to the DB. 我正在动态创建一些JSON,对其进行序列化并将其保存到数据库中。 To run it, i create a script element, and load it that way. 要运行它,我创建了一个脚本元素,并以这种方式加载它。 Is there a way to load the script source to a textarea? 有没有办法将脚本源加载到文本区域?

You don't "run" JSON, JSON is a data notation. 您无需“运行” JSON, JSON是一种数据符号。

Yes, you can put the text of a script tag into a text area, something along these lines: 是的,您可以将script标记的文本放入文本区域,具体方法如下:

var script, tb, node;
script = document.getElementById('theScriptID');
tb = document.getElementById('theTextBoxID');
tb.innerHTML = script.innerHTML;

Example: 例:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script id='theScriptID' type='text/javascript'>

    function go() {
        var script, tb;
        script = document.getElementById('theScriptID');
        tb = document.getElementById('theTextBoxID');
        tb.innerHTML = script.innerHTML;
    }

</script>
</head>
<body>
<textarea id='theTextBoxID' rows='20' cols='70'></textarea>
<input type='button' id='btnGo' value='Go' onclick="return go();">
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM