根据您在评论中提供的网站,我使脚本适合您的需求和元素:
我建议disabled="disabled"
在最终代码 textarea 上设置,以便用户可以选择,但不能编辑内容
<form method="get">
<label for="name">Name: <input type="text" id="name" placeholder="Victor Erixon"/></label><br/>
<label for="slogan">Slogan: <input type="text" id="slogan" placeholder="Interface Designer"/></label><br/>
<label for="bio">Bio: </label><br>
<textarea id="bio" placeholder="Write something about yourself"></textarea><br/>
<label for="favicon">Favicon: <input type="text" id="favicon" placeholder="http://www.gravatar.com/avatar/b20f810469b0.png"/></label><br/>
<label for="dribble">Dribble: <input type="text" id="dribble" placeholder="victorerixon"/></label><br/>
<textarea class="code" disabled="disabled"></textarea><br/>
<input type="submit" value="Submit"/>
</form>
var basicHtml = '<html>\n <head>\n <title>%name%</title>\n <meta name="description" content="%bio%">\n <link rel="icon" href="%favicon%">\n </head>\n <body>\n <h1>%name%</h1>\n <h2>%slogan%</h2>\n <ul id="portfolio">%dribbble%</ul>\n <p class="about">%bio%</p>\n </body>\n</html>';
$(document).ready(function(){
$('.code').val(basicHtml);
$('form').on('submit',function() {
var content = basicHtml;
content = content.replace(/%name%/g, ($('#name').val()||$('#name').attr('placeholder')));
content = content.replace(/%slogan%/g, ($('#slogan').val()||$('#slogan').attr('placeholder')));
content = content.replace(/%bio%/g, ($('#bio').val()||$('#bio').attr('placeholder')));
content = content.replace(/%favicon%/g, ($('#favicon').val()||$('#favicon').attr('placeholder')));
var final = content.replace(/%dribble%/g, ($('#dribble').val()||$('#dribble').attr('placeholder')));
$('.code').val(final);
return false;
}).on('reset',function(){
$('form textarea:not(.code),form input').val();
$('.code').val(basicHtml);
});
});
JSFIDDLE
无需定义basicHtml
:
您将始终必须像这样编写 textarea 的代码:(在开始/结束<html>
和<textarea>
标签之间没有空格或制表符)
<textarea class="code" disabled="disabled"><html>
<!-- rest of the code -->
</html></textarea>
并将代码更改为:
$(document).ready(function(){
var basicHtml = ($('.code').html()).replace(/</g,'<').replace(/>/g,'>');
//rest of code
});
JSFIDDLE