0
我有這個jQuery編輯textarea中的代碼,從內容插入字段。你可以看到它生活here。問題是,如果用戶沒有將某些東西放入其中一個字段,則佔位符會插入到代碼中。相反,我希望常規標識符留在那裏,而不是被替換,如果該字段留空。停止佔位符被插入代碼
這裏是jQuery代碼。
$(document).ready(function(){
var basicHtml = ($('.code').html()).replace(/</g,'<').replace(/>/g,'>');
$('.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(/%description%/g, ($('#description').val()||$('#description').attr('placeholder')));
content = content.replace(/%bio%/g, ($('#bio').val()||$('#bio').attr('placeholder')));
content = content.replace(/%analytics%/g, ($('#analytics').val()||$('#analytics').attr('placeholder')));
content = content.replace(/%favicon%/g, ($('#favicon').val()||$('#favicon').attr('placeholder')));
var final = content.replace(/%dribbble_username%/g, ($('#dribbble').val()||$('#dribbble').attr('placeholder')));
$('.code').val(final);
return false;
}).on('reset',function(){
$('form textarea:not(.code),form input').val();
$('.code').val(basicHtml);
});
});
謝謝!
這很完美!謝謝! – 2013-04-04 02:55:02