开发者

jQuery Client-Side Text Input Previews: Parsing

For the sake of learning more about jQuery, I was wondering if anyone could show me how to make a reaaallly simple parser in jQuery. All I would want the parser to do is take a post from a user and wherever the user has input a line break, make paragraphs.

Example: The user types two lines of a message:

 This is the first line.

 This is the second line.

What I would want out:

 <p>This is the first line.</p>

 <p>This is the second line.</p>

Let's say I define the parser as simpleParser (which currently doesn't alter the text):

 function simpleParser(text_input) {
      return text_input
 }

And it's called as such:

 $('.textarea').bind('keyup', function() {
    var post = $(this).val()
    $('#textarea .preview').html(textileParser(post));
});

I know that I can rely o开发者_Python百科n premade WYSIWYG and other editors that others have created, but as an experiment in learning more jQuery, can anyone help me out or point me in the right direction?


Try this:

function parseText(str){
    return "<p>" + str.split("\n").join("</p><p>") + "</p>";
}

here's a jsfiddle: http://jsfiddle.net/QKf3q/1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜