How to append or wrap text around specific tag

回眸只為那壹抹淺笑 提交于 2019-12-13 04:37:36

问题


In the select menu I have various bbcodes. I was wondering how I can wrap/append the user input from #message inside of a bbcode tag like this [b]USER-HAS-TYPED-SOMETHING[/b] and output that end results to <pre id="display"></pre>

DEMO - http://jsfiddle.net/kwicher/ypmfK/2/


回答1:


   $(document).ready(function() {
    $('#enableuserreply').click(function() {

        var dis = $(this).prop('checked') ? "" : "disabled";

        $("#usernames").prop('disabled', dis);
    });
    $('#enapletexteffect').click(function() {

        var dis = $(this).prop('checked') ? "" : "disabled";

        $("#boxcode").prop('disabled', dis);
        $("#colors").prop('disabled', dis);
    });

    $('#message').keyup(function(){   

        if ($('#enableuserreply').is(':checked')) {
            var msg = $("#usernames option:selected").text() + " has typed ";

            var boxcode = $("#boxcode option:selected").attr('value');

            var  message = $('#message').val();
            msg = msg + message ;

            var boxcodeArr = new Array();

            if(boxcode){
                boxcode = $.trim(boxcode.replace('[', '<'));
                boxcode = $.trim(boxcode.replace('[/', ',</'));
                boxcode = $.trim(boxcode.replace(']', '>'));
                boxcode = $.trim(boxcode.replace(']', '>'));
                boxcodeArr = boxcode.split(',');  
                 msg = boxcodeArr[0] + msg + boxcodeArr[1] ;         
            }

            $("#display").html(msg);  
            var color = $("#colors option:selected").attr('value');

            if(color) {
               $("#display").css('color', color);
            }


        }

    });
});

this is the complete code . it is working :)



来源:https://stackoverflow.com/questions/6092484/how-to-append-or-wrap-text-around-specific-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!