如何使用Javascript来操作模态内容?

我正在使用bootstrap模式和Ruby on Rails。 我能够显示模态罚款,但我无法使用Javascript来操作模态的内容。 我不确定我做错了什么,但我根本无法使用Javascript来影响模态的内容,直到我开始想知道DOM元素上是否存在不同的堆栈级别或者是否存在这样的事情甚至模态内容都是DOM的一部分。

这是我的app / views / home / index.haml:

- url = @entry.class.to_s.downcase.singularize = link_to(send("#{url}_path", @entry), remote: true) do = yield #modals 

视图包含打开模态的链接,在这种情况下,@ entry表示图像对象。 在图像控制器中我们有show动作,我用它来显示模态:

 def show authorize! :view, @image @can_destroy = can?('delete_asset', @image) @can_edit = can?('edit_metadata', @image) respond_to do |format| format.js end end 

对于模态视图,我有app / view / show.js.erb

 $("#modals").html(''); $('#modals .modal').modal(); $('#modals .modal').modal('.toggle'); 

最后,我在app / views / images / _modal.haml中有模态部分

 .modal .modal-dialog .modal-content .modal-header = image_tag(@image.file_original.url(:modal)) .modal-body #shortdesc.row .col-md-6 short description: #{@image.description_short} .col-md-6.rightButton %a.detailers %span#toggle-text SHOW DETAILS %ul.errors #detail.details{:style => "display: none"} %div.modal-details %i media type: = @image.media_type %br/ %div.modal-details %i subject: = @image.subject %br/ %div.modal-details %i title: = @image.title %br/ %div.modal-details %i full description: = @image.description %br/ %div.modal-details %i location: = @image.location %br/ %div.modal-details %i date: %br/ %div.modal-details %i author: = @image.author %br/ %div.modal-details %i source: = @image.source %br/ %div.modal-details %i tags: = @image.tag_list.join(' - ') %br/ %br/ %div.modal-detailsgreen This item is shared by %span.sharedbyfirstname = @image.user.name_first %span.sharedbylastname = @image.user.name_last %br/ %br/ 

现在我想隐藏并显示(切换)div的内容,当点击带有class =“detailers”的链接时,id =“detail”和class =“details”。 我试图在show.js.erb中编写Javascript来操作模态的内容但没有成功。 是否有某个地方我应该把我的JS用来操纵JS的内容?

 $('#modals .modal').on('shown.bs.modal', function () { // do something… });