2つのメインのJSスクリプトがあります。どちらも期待どおりのWORKです。その1つはメニュー用です)divを動的に変更する(HTMLの下で) 両方とも使用$(document).ready(function()
別々のファイルに入れても機能しません。同じ機能をすべて同じにしてください$(document).ready(function()
まだ動作しません。 If 私はJSを初めて使っているので、コードをどのようにレイアウトするかについて頭を悩ましているのですが、私はそれらを2つの独立したスクリプトとして同じファイルに入れます。
JS:
(function ($) {
"use strict";
var mainApp = {
main_fun: function() {
$('#main-menu').metisMenu();
$(window).bind("load resize", function() {
if ($(this).width() < 768) {
$('div.sidebar-collapse').addClass('collapse')
} else {
$('div.sidebar-collapse').removeClass('collapse')
}
});
},
initialization: function() {
mainApp.main_fun();
}
}
$(document).ready(function() {
mainApp.main_fun();
});
}(jQuery));
SECOND JS
$(document).ready(function(){
var $content = $('.menu-content');
function showContent(type) {
$($content).hide();
$('#'+type).show();
}
$('.menu').on('click', '.menu-btn', function(e) {
showContent(e.currentTarget.hash.slice(1));
e.preventDefault();
});
showContent('about');
});
HTML:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src= "http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="{{ url_for('static',filename='js/menu/custom.js') }}"></script>
<style>
div{
height: 200px;
width: 50%;
background-color: powderblue;
}
</style>
</head>
<body>
<ul class="menu">
<li><a href="#about" class="menu-btn">About</a></li>
<li><a href="#contact" class="menu-btn">Contact</a></li>
<li><a href="#cluster" class="menu-btn">cluster</a></li>
</ul>
<div id="about" class="menu-content">This is static - About</div>
<div id="contact" class="menu-content">This is static - Contact</div>
<div id="cluster" class="menu-content">cluster</div>
</body>
</html>
Expected Result - When I click on the buttons the single div should change
あなたが正確に動作しないかを指定する必要があります。 – 31piy
複数のドキュメント・レディ・ハンドラが問題を引き起こす原因となる本質的な理由はありません。この問題は、クロススクリプトの依存関係または実行順序の可能性が高くなります。何がうまくいかないか教えてください - 例えばコンソールのエラー?そうでなければ、それはかなり曖昧な声明です。 – Utkanos
編集を参照してください、コンソールにエラーはありません。 – Macbook