【问题标题】:jQuery UI + RailsjQuery UI + Rails
【发布时间】:2011-11-20 03:27:01
【问题描述】:

我的网站上的 haml 文件中有以下代码:

= stylesheet_link_tag "web-app-theme/base2", "web-app-theme/themes/activo/style", "web-app-theme/override", 'web-app-theme/tabs'
= javascript_include_tag 'tabber.js', 'jquery-1.3.2.min.js', 'jquery-ui-1.7.custom.min.js'
:javascript
  $(function() {

    var $tabs = $('#tabs').tabs();

    $(".ui-tabs-panel").each(function(i){

      var totalSize = $(".ui-tabs-panel").size() - 1;

      if (i != totalSize) {
          next = i + 2;
          $(this).append("Next Page »");
      }

      if (i != 0) {
          prev = i;
          $(this).append("« Prev Page");
      }

    });

    $('.next-tab, .prev-tab').click(function() { 
             $tabs.tabs('select', $(this).attr("rel"));
             return false;
         });


  });
#page-wrap
  #tabs
    %ul
      %li
        %a{:href => "#fragment-1"} 1
      %li
        %a{:href => "#fragment-2"} 2
      %li
        %a{:href => "#fragment-3"} 3
      %li
        %a{:href => "#fragment-4"} 4
      %li
        %a{:href => "#fragment-5"} 5
      %li
        %a{:href => "#fragment-6"} 6
      %li
        %a{:href => "#fragment-7"} 7
      %li
        %a{:href => "#fragment-8"} 8
      %li
        %a{:href => "#fragment-9"} 9
      %li
        %a{:href => "#fragment-10"} 10
      %li
        %a{:href => "#fragment-11"} 11
      %li
        %a{:href => "#fragment-12"} 12
      %li
        %a{:href => "#fragment-13"} 13
      %li
        %a{:href => "#fragment-14"} 14
      %li
        %a{:href => "#fragment-15"} 15
    #fragment-1.ui-tabs-panel
      %p Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
    #fragment-2.ui-tabs-panel.ui-tabs-hide
      %p Donec ultricies senectus tristique egestas vitae, et ac morbi habitant quam sit mi quam, malesuada leo. Vestibulum tempor Mauris tortor libero eget, egestas. eu vitae feugiat netus amet Pellentesque ante. amet, ultricies eleifend turpis sit placerat et semper. Aenean est. fames
    #fragment-3.ui-tabs-panel.ui-tabs-hide
      %p ante. Mauris Vestibulum est. fames egestas quam, leo. amet tristique sit libero egestas. ultricies mi turpis senectus Pellentesque habitant eu ac morbi netus eget, Aenean malesuada vitae, semper. eleifend et feugiat vitae amet, placerat Donec et tortor ultricies tempor quam sit
    #fragment-4.ui-tabs-panel.ui-tabs-hide
    #fragment-5.ui-tabs-panel.ui-tabs-hide
    #fragment-6.ui-tabs-panel.ui-tabs-hide
    #fragment-7.ui-tabs-panel.ui-tabs-hide
    #fragment-8.ui-tabs-panel.ui-tabs-hide
    #fragment-9.ui-tabs-panel.ui-tabs-hide
    #fragment-10.ui-tabs-panel.ui-tabs-hide
    #fragment-11.ui-tabs-panel.ui-tabs-hide
    #fragment-12.ui-tabs-panel.ui-tabs-hide
    #fragment-13.ui-tabs-panel.ui-tabs-hide
    #fragment-14.ui-tabs-panel.ui-tabs-hide
    #fragment-15.ui-tabs-panel.ui-tabs-hide
      %p The end.

代码很好,因为它创建了正确的选项卡界面,但是按钮不起作用,它们只是显示为文本,所以我想知道是否应该做些什么来使 javascript 正常工作?我尝试删除haml文件中的javascript代码并将其放入application.js:

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
$(function() {

var $tabs = $('#tabs').tabs();

$    (".ui-tabs-panel").each(function(i){

  var totalSize = $(".ui-tabs-panel").size() - 1;

  if (i != totalSize) {
      next = i + 2;
      $(this).append("Next Page »");
  }

  if (i != 0) {
      prev = i;
      $(this).append("« Prev Page");
  }

});

$('.next-tab, .prev-tab').click(function() { 
         $tabs.tabs('select', $(this).attr("rel"));
         return false;
     });


});

这只会导致选项卡布局中断并且不再正常工作。

【问题讨论】:

    标签: javascript ruby-on-rails jquery-ui tabs haml


    【解决方案1】:

    所以问题是来自 jquery 站点的原始代码都是 html - 当它转换为 haml 时,它还从 javascript 部分中删除了 html。然后会破坏 javascript 链接正常工作。吸取的教训不要通过haml2html运行javascript。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 2014-09-14
      相关资源
      最近更新 更多