Test if cursor is on the first line in a textarea (with soft-newlines)

前端 未结 2 890
小蘑菇
小蘑菇 2021-01-17 21:32

Given a textarea with content that flows like this

     ––––––––––––––––––––––––––
    | This is some text, which |
    | wraps like this.         |
     ––––––––         


        
2条回答
  •  醉话见心
    2021-01-17 22:36

    Having that 15 is the line height, this works (tested in firefox):

    http://jsfiddle.net/h46jh/12/

    $("textarea").click(function (evt) {
      cursor_position = evt.pageY - $('textarea').offset().top;
      if (cursor_position <= 15) {
        alert("first line");
      } else {
        alert("other line");
      }
    });
    

    Credits:

    Find mouse position relative to element

提交回复
热议问题