javascript - 如何在服务器端设置闪亮的小部件 css?
问题描述
我的目标是通过每次点击来更改 actionButton css,有没有一种方法可以在不使用 shinyBS 和 shinyjs 的情况下实现?
请参阅下面的最小可重现示例。
library(shiny)
shinyApp(
ui = fluidPage(
tags$style("#action{color:black;}"),
actionButton("action", label = "Action")
),
server = function(input, output) {
observeEvent(input$action, {
message(input$action %% 2)
if (input$action %% 2) {
tags$style("#action{color:red;}")
} else {
tags$style("#action{color:black;}")
}
})
}
)
点击 1 红色
点击 2 黑色
点击 3 红色
点击 4 黑色
点击 5 红色
...
解决方案
一种 JavaScript 方式:
js <- "
function changeColor(button){
var currentColor = button.style.color;
var newColor = currentColor === 'red' ? 'black' : 'red';
button.style.color = newColor;
}
"
ui <- basicPage(
tags$head(tags$script(HTML(js))),
actionButton(
"btn", "Click me", style = "color: red;",
onclick = "changeColor(this)"
)
)
server <- function(input, output, session){}
shinyApp(ui, server)
推荐阅读
- ruby-on-rails - 我要运行哪个迁移来更改此行:t.index ["email"], name: "index_users_on_email", unique: true
- python - 如何将文件夹中的多张图片上传到 S3 Bucket?
- python - 你如何防止一条线相交
- python - 如果连续按下 2 次,Python Tkinter 会使返回键执行不同的功能
- java - 如何从列表的确定位置查找和求和 n 个值
- eslint - GitHub ESLint action failing with "Permission Denied"
- uwp - UWP 绘制图像并保存到 DB
- python - 如何在plotly中更改等高线图中的轴刻度距离
- microsoft-dynamics - Power Automate Web API GET 间歇性失败
- hive - 如何水平拆分大型 Hive 表