在输入按

时间:2016-06-03 12:25:02

标签: javascript jquery html css

我有contenteditable div,我想按Enter键,在文本中添加new line字符。

我这样做:

$("#editable").on("keydown", function(e) {
    if (e.keyCode === 13) {
        document.execCommand('insertHTML', false, '\n');
    }
});

这样做有效,但这也会在文本中添加<br>标记(并在chrome <div>标记中添加)

我需要阻止在输入时添加此标签。

我知道两种方式:

1)使用preventDefault()方法,但是使用此方法,用户需要按两次Enter键进行换行。这很糟糕。

2)使用replace()方法从全文中删除此标记,但在这种情况下,所有这些标记(如果用户在文本中特别输入)也将被删除,因此这也是不好的方法。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

试试这个:

[ { coursesession_id: 574fc6baa09d9bc4050b974e,
        student_id: 574fbf10179281fc15c5db37,
        tutor_id: 574fbff2179281fc15c5db3d,
        aproval_status: 'pending',
        start_date: Thu Jun 02 2016 11:00:00 GMT+0530 (India Standard Time),
        duration: '06:00',
        session_type: 'One To One',
        enteredtopic_name: 'addition',
        payment_status: 'paid',
        course_completed: false,
        TxnId: 'IKGqrpI6IHnxIFEJsFt53EIqw',
        id: 574fc6baa09d9bc4050b974f,
        StudentRelation:
         { user_type: 'student',
           firstname: 'Student',
           lastname: 'Ps',
           active_status: true,
           email: 'sikha.p91@gmail.com',
           grade: '574fbeb2179281fc15c5db34',
           syllabus: '574fbea9179281fc15c5db33',
           mobile: '8157897518',
           place: 'kannru',
           password: '$2a$10$YIUVCclA6.vBeePZuMY7uu.6lYr..fQD4o9dSY7uF9FmgkTWAqq3i',

           emailVerified: true,
           id: 574fbf10179281fc15c5db37 } },
      { coursesession_id: 574fde1dc59f447c1235007b,
        student_id: 574fbf10179281fc15c5db37,
        tutor_id: 574fbff2179281fc15c5db3d,
        aproval_status: 'pending',
        start_date: Mon Jun 13 2016 09:00:00 GMT+0530 (India Standard Time),
        duration: '01:00',
        session_type: 'One To One',
        enteredtopic_name: 'integration',
        payment_status: 'paid',
        course_completed: false,
        TxnId: 'KpDGHtLwIK6D3zIICtIGoL46p',
        id: 574fde1dc59f447c1235007c,
        StudentRelation:
         { user_type: 'student',
           firstname: 'Student',
           lastname: 'Ps',
           active_status: true,
           email: 'sikha.p91@gmail.com',
           grade: '574fbeb2179281fc15c5db34',
           syllabus: '574fbea9179281fc15c5db33',
           mobile: '8157897518',
           place: 'kannru',
           password: '$2a$10$YIUVCclA6.vBeePZuMY7uu.6lYr..fQD4o9dSY7uF9FmgkTWAqq3i',

           emailVerified: true,
           id: 574fbf10179281fc15c5db37 } },
      { coursesession_id: 57511d25388d2150161559de,
        student_id: 574fbf10179281fc15c5db37,
        tutor_id: 574fbff2179281fc15c5db3d,
        aproval_status: 'pending',
        start_date: Fri Jun 03 2016 09:00:00 GMT+0530 (India Standard Time),
        duration: '06:00',
        session_type: 'One To One',
        enteredtopic_name: 'multiplication',
        payment_status: 'paid',
        course_completed: false,
        TxnId: 'voyn9oqyG14ItzzwqHI4GxM1o',
        id: 57511d25388d2150161559df,
        StudentRelation:
         { user_type: 'student',
           firstname: 'Student',
           lastname: 'Ps',
           active_status: true,
           email: 'sikha.p91@gmail.com',
           grade: '574fbeb2179281fc15c5db34',
           syllabus: '574fbea9179281fc15c5db33',
           mobile: '8157897518',
           place: 'kannru',
           password: '$2a$10$YIUVCclA6.vBeePZuMY7uu.6lYr..fQD4o9dSY7uF9FmgkTWAqq3i',

           emailVerified: true,
           id: 574fbf10179281fc15c5db37 } },
      { coursesession_id: 57511d25388d2150161559de,
        student_id: 574fbf10179281fc15c5db37,
        tutor_id: 574fbff2179281fc15c5db3d,
        aproval_status: 'pending',
        start_date: Sat Jun 04 2016 09:00:00 GMT+0530 (India Standard Time),
        duration: '06:00',
        session_type: 'One To One',
        enteredtopic_name: 'multiplication',
        payment_status: 'paid',
        course_completed: false,
        TxnId: 'voyn9oqyG14ItzzwqHI4GxM1o',
        id: 57511d25388d2150161559e0,
        StudentRelation:
         { user_type: 'student',
           firstname: 'Student',
           lastname: 'Ps',
           active_status: true,
           email: 'sikha.p91@gmail.com',
           grade: '574fbeb2179281fc15c5db34',
           syllabus: '574fbea9179281fc15c5db33',
           mobile: '8157897518',
           place: 'kannru',
           password: '$2a$10$YIUVCclA6.vBeePZuMY7uu.6lYr..fQD4o9dSY7uF9FmgkTWAqq3i',

           emailVerified: true,
           id: 574fbf10179281fc15c5db37 } } ]