Javascript sending key codes to a <textarea> element

前端 未结 5 1018
说谎
说谎 2020-12-18 14:58

I can\'t figure out how to trigger a keydown event on a textarea element, e.g. imagine i have two textarea elements and when i type something in the first one, I want the se

5条回答
  •  生来不讨喜
    2020-12-18 15:24

    Try this:

    $(document).ready(function() {
    var $comment = '';
    $('#first').keyup(function() {
        $comment = $(this).val();
        $comment = $comment.replace(/<\/?[^>]+>/g,"").replace(/\n/g, "
    ").replace(/\n\n+/g, '

    '); // this strips tags and then replaces new lines with breaks $('#second').html( $comment ); }); });

    Working demo: http://jsbin.com/ivulu

    Or if you don't want to sanitize the data: http://jsbin.com/oposu

    $(document).ready(function() {
    var $comment = '';
    $('#first').keyup(function() {
        $comment = $(this).val();
        $('#second').html( $comment );
    });
    });
    

提交回复
热议问题