2017-09-07 22 views
8

Tôi muốn chụp một nhấp chuột vào liên kết trong iframe trong ứng dụng sáng bóng. Và tôi muốn biết liên kết nào được nhấp vào.Chụp nhấp trong iframe trong ứng dụng sáng bóng

Làm nổi bật bên ngoài công việc này. Tôi đã thêm một ví dụ hoàn toàn có thể tái sản xuất cho một câu hỏi liên quan: https://stackoverflow.com/a/46093537/3502164 (Nó phải chạy trên máy chủ (cục bộ), ví dụ: xaamp).

Nỗ lực của tôi:

1) Ứng dụng được lưu trong Path/To/App.

2) Trong thư mục www lưu trữ tệp html sẽ được hiển thị trong khung nội tuyến.

fileWithLink.html

<html> 
<body> 
<a href="https://stackoverflow.com/">SOreadytohelp</a> 
</body> 
</html> 

3) App phải được bắt đầu với runApp("Path/To/App", launch.browser = TRUE) (Vì vậy mà một máy chủ cục bộ được bắt đầu).

library(shiny) 
library(shinyjs) 

ui <- fluidPage(
    useShinyjs(), 
    htmlOutput("filecontainer") 
) 

server <- function(input, output, session){ 
    session$onFlushed(once = T, function(){ 
     runjs(" 
      console.log('I arrive here') 
      $('#filecontainer').load(function(){ 
      console.log('But not here') 
      var iframe = $('#filecontainer').contents(); 
      iframe.find('#a').click(function(){ 
       alert('I want to arrive here'); 
      }); 
      }); 
     ") 
    }) 

    output$filecontainer <- renderUI({ 
    tags$iframe(src = "fileWithLink.html", height = 600, width = 1200) 
    }) 
} 

shinyApp(ui, server) 

Trả lời

5

Khung nội tuyến được đính kèm trong div có id liên quan ($('#filecontainer iframe')). Có một lỗi đánh máy gọi là các thẻ neo. Tôi đã thay đổi địa điểm để tránh các vấn đề kịch bản chéo:

library(shiny) 
library(shinyjs) 

ui <- fluidPage(
    useShinyjs(), 
    htmlOutput("filecontainer") 
) 

server <- function(input, output, session){ 
    session$onFlushed(once = T, function(){ 
    runjs(" 
      console.log('I arrive here') 
      $('#filecontainer iframe').load(function(){ 
      console.log('But not here') 
      var iframe = $('#filecontainer iframe').contents(); 
      iframe.find('a').click(function(){ 
       console.log('am i here') 
       alert('I want to arrive here'); 
      }); 
      }); 
      ") 
    }) 

    output$filecontainer <- renderUI({ 
    tags$iframe(src = "fileWithLink.html", height = 600, width = 1200) 
    }) 
    } 

shinyApp(ui, server) 

fileWithLink.html

<html> 
<body> 
<a href="anotherpage.html">SOreadytohelp</a> 
</body> 
</html> 

anotherpage.html

<html> 
<body> 
Another page 
</body> 
</html> 
Các vấn đề liên quan