简体   繁体   English

更换 <br /> 与\\ n

[英]Replacing <br /> with \n

I am playing around with this page: http://www.problemio.com/problems/problem.php?problem_id=228 我正在玩此页面: http : //www.problemio.com/problems/problem.php?problem_id=228

To reproduce the problem, log in with this: testing@problemio.com / testing 要重现该问题,请使用以下身份登录:testing@problemio.com / testing

Then click on the "comments" tab and click on edit wherever you see that option inside the edits tab. 然后,单击“评论”选项卡,然后在“编辑”选项卡中看到该选项的任何地方单击“编辑”。 You will see that the <br /> are still there, but to create that edit form, here is the JavaScript I use to make the html for that form: 您将看到<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;
});     

So you see I try to replace the <br /> with \\n and it kind of works, but if you do the same thing again with the same comment, it places </a> tag in there. 因此,您看到我尝试用\\n替换<br /> ,并且可以正常工作,但是如果再次使用相同的注释执行相同的操作,则会在其中放置</a>标记。 Any idea why that happens and how to stop it? 知道为什么会发生以及如何阻止它吗? Its weird because it happens on the second time editing a comment and not the first. 这很奇怪,因为它是第二次编辑评论,而不是第一次。

You can do this to help you replace <br> with \\n : 您可以这样做以帮助您将<br>替换为\\n

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

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM