【问题标题】:How to use Javascript to manipulate modal content?如何使用 Javascript 来操作模态内容?
【发布时间】:2017-01-26 16:00:28
【问题描述】:

我正在使用引导模式和 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('<%= j render "images/modal", image: @image %>');
$('#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/

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

【问题讨论】:

    标签: javascript ruby-on-rails haml bootstrap-modal


    【解决方案1】:
    $('#modals .modal').on('shown.bs.modal', function () {
      // do something… 
    });
    

    【讨论】:

    • 那么首先我应该在 show.js.erb 还是 home.js 上使用它?其次,您将如何选择带有“.detailer”类的链接。你会使用 $(' #modals .modal' a .detailer ) 吗?您能否详细说明您的答案,并至少指定您将在何处编写该答案以及如何选择带有 .detailers 类的链接?
    • 请编辑更多信息。不建议使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。
    猜你喜欢
    • 2011-04-22
    • 2013-04-08
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2020-08-12
    • 2019-02-12
    相关资源
    最近更新 更多