gpt4 book ai didi

javascript - Bootstrap 自定义 JavaScript 文件未加载(Rails 4)

转载 作者:行者123 更新时间:2023-11-28 07:33:19 25 4
gpt4 key购买 nike

SOLVED: See my answer below

我的最终目标是在我的应用程序中构建一个 Heroku 仪表板样式的可折叠侧边栏(将 heroku 缩小到大约 768 像素,然后它就会出现)。然而,我每次都失败了。我使用的是 Rails 4 和 Bootstrap 3,它们似乎不具备开箱即用的能力。

我的 Rails 应用究竟出了什么问题?我无法在此处加载任何需要 JS 的内容。我试过将我的 JS 包装在 document.ready 中但这也没有用。

我一直在关注这个[诚然很旧,写得不是很好]指南: http://seegatesite.com/create-simple-cool-sidebar-menu-with-bootstrap-3/

我有 gem 'jquery-rails在我的 Gemfile ,并在我的 config/initializers/assets.rb 中包含以下内容:

Rails.application.config.assets.precompile += %w( app.js )
Rails.application.config.assets.precompile += %w( app.css )

我什至尝试将所需的 JS 插入 <script> inside the .html.erb` 文件无效。对我有什么线索或有用的链接吗?

似乎所有 HTML 和 CSS 都运行良好,但切换按钮没有任何功能。

这是我的 app.js :

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
//= require highcharts/highstock
//= require highcharts/highcharts
//= require highcharts/highcharts-more
//= require highcharts/modules/exporting

/* off-canvas sidebar toggle */

$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
$("#menu-toggle-2").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled-2");
$('#menu ul').hide();
});

function initMenu() {
$('#menu ul').hide();
$('#menu ul').children('.current').parent().show();
//$('#menu ul:first').show();
$('#menu li a').click(
function() {
var checkElement = $(this).next();
if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
return false;
}
if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
$('#menu ul:visible').slideUp('normal');
checkElement.slideDown('normal');
return false;
}
}
);
}
$(document).ready(function() {initMenu();});

我的 app.css.scss :

/*!
* Start Bootstrap - Simple Sidebar HTML Template (http://startbootstrap.com)
* Code licensed under the Apache License v2.0.
* For details, see http://www.apache.org/licenses/LICENSE-2.0.
*/

/* Toggle Styles */
/* This sidebar modified by seegatesite.com */

/*
*
*= require_tree .
*= require_self
*/
@import "bootstrap-sprockets";
@import "bootstrap";


#wrapper {
padding-left: 0;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}

#wrapper.toggled {
padding-left: 250px;
}

#sidebar-wrapper {
z-index: 1000;
position: fixed;
left: 250px;
width: 0; /* disini agar ketika di kecilkan tidak hilang semua default 0*/
height: 100%;
margin-left: -250px;
overflow-y: auto;
background: #000;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#wrapper.toggled #sidebar-wrapper {
width: 250px;
}

#page-content-wrapper {
width: 100%;
position: absolute;
padding: 15px;
}

#wrapper.toggled #page-content-wrapper {
position: absolute;
margin-right: -250px;
}
.fixed-brand{
width: auto;
}
/* Sidebar Styles */

.sidebar-nav {
position: absolute;
top: 0;
width: 250px;
margin: 0;
padding: 0;
list-style: none;
margin-top: 2px;
}

.sidebar-nav li {
text-indent: 15px;
line-height: 40px;
}

.sidebar-nav li a {
display: block;
text-decoration: none;
color: #999999;
}

.sidebar-nav li a:hover {
text-decoration: none;
color: #fff;
background: rgba(255,255,255,0.2);
border-left: red 2px solid;
}

.sidebar-nav li a:active,
.sidebar-nav li a:focus {
text-decoration: none;
}

.sidebar-nav > .sidebar-brand {
height: 65px;
font-size: 18px;
line-height: 60px;
}

.sidebar-nav > .sidebar-brand a {
color: #999999;
}

.sidebar-nav > .sidebar-brand a:hover {
color: #fff;
background: none;
}
.no-margin{
margin:0;
}

@media(min-width:768px) {
#wrapper {
padding-left: 250px;
}
.fixed-brand{
width: 250px;
}
#wrapper.toggled {
padding-left: 0;
}

#sidebar-wrapper {
width: 250px;
}

#wrapper.toggled #sidebar-wrapper {
width: 250px;
}
#wrapper.toggled-2 #sidebar-wrapper {
width: 50px;
}
#wrapper.toggled-2 #sidebar-wrapper:hover {
width: 250px;
}

#page-content-wrapper {
padding: 20px;
position: relative;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}

#wrapper.toggled #page-content-wrapper {
position: relative;
margin-right: 0;
padding-left: 250px;

}
#wrapper.toggled-2 #page-content-wrapper {
position: relative;
margin-right: 0;
margin-left: -200px;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
}

最后是我的 app.html.erb

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<%= stylesheet_link_tag 'app', media: 'all' %>
<%= javascript_include_tag 'app' %>
</head>
<body>
<nav class="navbar navbar-default no-margin">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header fixed-brand">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" id="menu-toggle">
<span class="glyphicon glyphicon-th-large" aria-hidden="true"></span>
</button>
<a class="navbar-brand" href="#"><i class="fa fa-rocket fa-4"></i> SEEGATESITE</a>
</div><!-- navbar-header-->

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active" ><button class="navbar-toggle collapse in" data-toggle="collapse" id="menu-toggle-2"> <span class="glyphicon glyphicon-th-large" aria-hidden="true"></span></button></li>
</ul>
</div><!-- bs-example-navbar-collapse-1 -->
</nav>
<div id="wrapper">
<!-- Sidebar -->
<div id="sidebar-wrapper">
<ul class="sidebar-nav nav-pills nav-stacked" id="menu">

<li class="active">
<a href="#"><span class="fa-stack fa-lg pull-left"><i class="fa fa-dashboard fa-stack-1x "></i></span> Dashboard</a>
<ul class="nav-pills nav-stacked" style="list-style-type:none;">
<li><a href="#">link1</a></li>
<li><a href="#">link2</a></li>
</ul>
</li>
<li>
<a href="#"><span class="fa-stack fa-lg pull-left"><i class="fa fa-flag fa-stack-1x "></i></span> Shortcut</a>
<ul class="nav-pills nav-stacked" style="list-style-type:none;">
<li><a href="#"><span class="fa-stack fa-lg pull-left"><i class="fa fa-flag fa-stack-1x "></i></span>link1</a></li>
<li><a href="#"><span class="fa-stack fa-lg pull-left"><i class="fa fa-flag fa-stack-1x "></i></span>link2</a></li>

</ul>
</li>
<li>
<a href="#"><span class="fa-stack fa-lg pull-left"><i class="fa fa-cloud-download fa-stack-1x "></i></span>Overview</a>
</li>
<li>
<a href="#"> <span class="fa-stack fa-lg pull-left"><i class="fa fa-cart-plus fa-stack-1x "></i></span>Events</a>
</li>
<li>
<a href="#"><span class="fa-stack fa-lg pull-left"><i class="fa fa-youtube-play fa-stack-1x "></i></span>About</a>
</li>
<li>
<a href="#"><span class="fa-stack fa-lg pull-left"><i class="fa fa-wrench fa-stack-1x "></i></span>Services</a>
</li>
<li>
<a href="#"><span class="fa-stack fa-lg pull-left"><i class="fa fa-server fa-stack-1x "></i></span>Contact</a>
</li>
</ul>
</div><!-- /#sidebar-wrapper -->
<!-- Page Content -->
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h1>Simple Sidebar With Bootstrap 3 by Seegatesite.com</h1>
<p>This sidebar is adopted from <a href="http://startbootstrap.com/">start bootstrap simple sidebar</a>, which I modified slightly to be more cool. For tutorials and how to create it , you can read from my site </p>
</div>
</div>
</div>
</div>
<!-- /#page-content-wrapper -->
</div>
<!-- /#wrapper -->
</body>
</html>

最佳答案

已通过将 JavaScript 文件引用移动到页面底部来修复。将更改问题标题,以便人们更容易找到这可能有所帮助。

<%= stylesheet_link_tag 'app', media: 'all' %>
<%= javascript_include_tag 'app' %>

关于javascript - Bootstrap 自定义 JavaScript 文件未加载(Rails 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com