Javascript 如何在有光泽的文本区域中获取光标位置输入

Javascript 如何在有光泽的文本区域中获取光标位置输入,javascript,r,shiny,Javascript,R,Shiny,有人知道我如何在一个闪亮的应用程序中,在textAreaInput中获取光标位置吗 library(shiny) ui <- fluidPage( textAreaInput("hop" ,label="textarea",value = "Supercalifragilisticexpialidocious"), verbatimTextOutput("out") ) server <- function(input, output, s

有人知道我如何在一个闪亮的应用程序中,在textAreaInput中获取光标位置吗

library(shiny)

ui <- fluidPage(
  textAreaInput("hop"
                ,label="textarea",value = "Supercalifragilisticexpialidocious"),
  verbatimTextOutput("out")
)

server <- function(input, output, session) {

  output$out <- renderText({
    "here I would like to get the cursor position (an interger?) \n inside que textArea"

  })

}

shinyApp(ui, server)
库(闪亮)

ui这是我找到的解决方案:

library(shiny)

ui <- fluidPage(tags$head(tags$script(
  'Shiny.addCustomMessageHandler("prout",
  function(NULL) {

   var ctl = document.getElementById("hop");
    var startPos = ctl.selectionStart;
  var endPos = ctl.selectionEnd;
  alert(startPos + ", " + endPos);

  });'
    )),
  textAreaInput("hop"
                ,label="textarea",value = "Supercalifragilisticexpialidocious"),
  verbatimTextOutput("out"),
  actionButton("hop","hop")
)

server <- function(input, output, session) {

  output$out <- renderText({
    "here I would like to get the cursor position (an interger?) \n inside que textArea"

  })

  observeEvent(input$hop,{
    message("hop")
    session$sendCustomMessage(type="prout",message=list(NULL))
  })
}

shinyApp(ui, server)
库(闪亮)
用户界面