0

我有这个 jQuery,它从插入到字段中的内容编辑文本区域中的代码。你可以在这里看到它。问题是,如果用户没有在其中一个字段中输入内容,占位符就会被插入到代码中。相反,我希望常规标识符留在那里,而不是在字段为空时被替换。

这是jQuery代码。

$(document).ready(function(){
    var basicHtml = ($('.code').html()).replace(/&lt;/g,'<').replace(/&gt;/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);
    });

});

谢谢!

4

1 回答 1

1

您基本上是通过读取占位符属性来设置它。固定代码:

$(document).ready(function(){
    var basicHtml = ($('.code').html()).replace(/&lt;/g,'<').replace(/&gt;/g,'>');

    $('.code').val(basicHtml);

    $('form').on('submit',function() {
        var content = basicHtml;
        content = content.replace(/%name%/g, ($('#name').val()));
        content = content.replace(/%slogan%/g, ($('#slogan').val()));
        content = content.replace(/%description%/g, ($('#description').val()));
        content = content.replace(/%bio%/g, ($('#bio').val()));
        content = content.replace(/%analytics%/g, ($('#analytics').val()));
        content = content.replace(/%favicon%/g, ($('#favicon').val()));
        var final = content.replace(/%dribbble_username%/g, ($('#dribbble').val()));
        $('.code').val(final);

        return false;
    }).on('reset',function(){
        $('form textarea:not(.code),form input').val();
        $('.code').val(basicHtml);
    });

});
于 2013-04-04T02:53:23.923 回答