用\ n替换<br/>

时间:2011-11-27 00:44:04

标签: javascript jquery

我正在玩这个页面:http://www.problemio.com/problems/problem.php?problem_id=228

要重现此问题,请登录:testing@problemio.com / testing

然后单击“评论”选项卡,然后在编辑选项卡中的任何位置单击编辑。您将看到<br />仍然存在,但要创建该编辑表单,这里是我用来为该表单创建html的JavaScript:

// Edit comment
$('.edit_comment').live('click' , function() 
{
    // Showing the wait image
    $("#loading").show();

    var problem_id = $(this).attr("data-problem_id");
    var problem_comment_id = $(this).attr("data-problem_comment_id");  
    var problem_comment_text = $(this).attr("data-problem_text");

    // problem_comment_text_'.$problem_comment_id.'
    var div_name = "problem_comment_text_" + problem_comment_id;
        //var dataString = 'problem_id='+ problem_id + '&problem_comment_id=' + problem_comment_id;

    // Now validate the input
        if( problem_id == '' || problem_comment_id == '' )
        {
       //$('#add_message_success').fadeIn(200).hide();
        //$('#add_message_error').fadeOut(200).show();          
        }
        else
        {   
            // Check if the person is logged in.
            // Now check if the person is logged in.
        $.ajax({
                type: "POST",
                url: "/auth/check_login.php",
                dataType: "json",
                success: function(data)
                {
                    $("#loading").hide();

                    // 1) close that piece of HTML
                    $("#" + div_name).hide();  // Works

                    problem_comment_text = problem_comment_text.replace(/\n/g,"<br />");

                // 2) Make an HTML form and display it in that piece of HTML
                var new_comment_form = "<form id='add_comment' method='post'><textarea name='problem_comment' cols=60 rows=6 id='problem_comment'>" + problem_comment_text + "</textarea><input type='hidden' id='problem_id' name='problem_id' value='" + problem_id + "' /><input type='hidden' id='problem_comment_id' value='" + problem_comment_id + "' /><input type='submit' class='button' value='Edit Message' /><input type='button' class='button' id='cancel_comment' data-problem_id='" + problem_id + "' value='Cancel' /></form>";

                // Now replace the current form with the crap I made above.
                $("#" + div_name).html( new_comment_form );  // Works
                $("#" + div_name).show(  );  // Works

                // 3) Hide the other text area form.
                $("#comment_bottom_text").hide(); // TODO - MAKE THIS WORK
                $(".comment_form").hide();                                              
            },
            error: function(json) // Error for checking if user is logged in.
            {
                // Showing the wait image
                $("#loading").hide();                    

                $("#loginpopup").dialog();

                return false;           
            }
    });
    }         

 return false;
});     

因此,您看到我尝试将<br />替换为\n并且它有点有用,但是如果您使用相同的评论再次执行相同的操作,则会将</a>标记放入那里。知道为什么会这样,以及如何阻止它?它很奇怪,因为它发生在第二次编辑评论而不是第一次。

1 个答案:

答案 0 :(得分:1)

您可以执行此操作来帮助您将<br>替换为\n

problem_comment_text.replace(/<br>/gi, '\n');

希望它有所帮助。