【问题标题】:JavaScript not working within modal in ShinyJavaScript 在 Shiny 的模态中不起作用
【发布时间】:2021-06-01 03:00:51
【问题描述】:

我在 Shiny 应用程序中使用了一些 JavaScript 代码,以便在模式中设置 textAreaInput 的样式。我的代码如下所示:

library(shiny)

codeJS <- "
  document.addEventListener('DOMContentLoaded', function(event)
  {                
    function init (inputID) 
    {
      var text = document.getElementById(inputID);
      text.style.color = 'blue';
    };
  
    init('textField');
  })"

ui <- fluidPage(
  tags$script(HTML(codeJS)),
  actionButton(inputId="openModal", label="Open modal")
)

server <- function(input, output, session)
{
  observeEvent(input$openModal,
  {
    showModal(modalDialog(
      textAreaInput(inputId = "textField", label = "window", value = "ABC")
    ))
  })
}

shinyApp(ui, server)

textAreaInput(inputId = "textField", label = "window", value = "ABC") 放在模态框之外时,JavaScript 代码可以正常工作。但是 JavaScript 代码对模态框内的输入没有影响。

有什么解决办法吗?

【问题讨论】:

    标签: javascript r shiny shinyjs


    【解决方案1】:

    YBS 让我走上了正轨。使用 JavaScript 而不是 CSS,代码变成这样:

    codeJS <- "
      function init (inputID) 
      {
        var text = document.getElementById(inputID);
            text.style.color     = 'red';
            text.style.fontSize  = '20px';
            text.style.fontStyle = 'italic';
      };
      
      init('textField');"
    
    ui <- fluidPage(
      actionButton(inputId="openModal", label="Open modal")
    )
    
    server <- function(input, output, session)
    {
      observeEvent(input$openModal,
      {
        showModal(modalDialog(
          textAreaInput(inputId = "textField", label = "window", value = "ABC"),
          tags$script(HTML(codeJS))
        ))
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 你能告诉你运行脚本时出了什么问题吗?
    • 您的回答很好。抱歉,我没有复制新的 codeJS 脚本,因为我认为您的原始 codeJS 没有更改。
    【解决方案2】:

    您可以包含如下所示的内联 CSS 代码。

    ui <- fluidPage(
      #tags$script(HTML(codeJS)),
      actionButton(inputId="openModal", label="Open modal")
    )
    
    server <- function(input, output, session)
    {
      observeEvent(input$openModal,
                   {
                     showModal(modalDialog(
                       textAreaInput(inputId = "textField", label = "window", value = "ABC"),
                       tags$head(tags$style("#textField{color: red;
                                     font-size: 20px;
                                     font-style: italic;
                                     }" ))
                     ))
                   })
    }
    
    shinyApp(ui, server)
    

    对于内联 javascript,试试这个

    ui <- fluidPage(
      actionButton(inputId="openModal", label="Open modal")
    )
    
    server <- function(input, output, session)
    {
      observeEvent(input$openModal,
                   {
                     showModal(modalDialog(
                       textAreaInput(inputId = "textField", label = "window", value = "ABC"),
                       tags$script(HTML('document.getElementById("textField").style.color = "red"'))
                     ))
                   })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 你让我走上了正轨。但我想使用 JavaScript 而不是 CSS。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2017-12-23
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多