gpt4 book ai didi

javascript - 如何在 Shiny textareaInput 中获取光标位置

转载 作者:行者123 更新时间:2023-11-29 23:21:32 25 4
gpt4 key购买 nike

有谁知道我如何才能在 Shiny 的应用程序中获取 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)

我想我必须使用 javascript,但我不知道从哪里开始。

问候

最佳答案

这是我找到的解决方案:

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)

关于javascript - 如何在 Shiny textareaInput 中获取光标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50430219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com