我有一个使用Bootstrap的选项卡窗格,我试图通过jQuery脚本加载外部页面以使用Ajax。
ajax脚本正在加载页面的内容,但窗格没有正确切换。当我点击它们时它们会加载,但它们只是“粘住”,当我点击它时第二个堆叠在第一个窗格下面。
这是HTML(注意,我使用的是Django,所以hrefs是Django查找)
<div class="container-fluid">
<div id="tabs">
<ul class="nav nav-tabs" id="caseTab">
<li class="active">
<a data-url="{% url 'person_case_list' person.id %}" href="#case_list">Case List</a>
</li>
<li>
<a data-url="{% url 'pledge_list' person.id %}" href="#pledge_list">Pledge List</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="case_list">
</div>
<div class="tab-pane active" id="pledge_list">
</div>
</div>
</div>
</div>
javascript是:
<script>
$('#caseTab a').click(function (e) {
var url = $(this).attr("data-url");
console.log(url);
if(typeof url !== "undefined") {
var pane = $(this), href = this.hash;
$(href).load(url, function(result){
pane.tab('show');
if (window.jQuery){
alert("Doing Number 1: jQuery working");
} else {
alert("Doing Number 1: jQuerty NOT working");
}
});
} else {
$(this).tab('show');
if (window.jQuery){
alert("Doing number 2: jQuery Working");
} else {
alert("Doing number 2: jQuery NOT working");
}
}
});
</script>
日志"Doing number 1: jQuery Working"
仅在我单击选项卡1(case_list
)时触发,但不是每次都会触发。偶尔会触发标签2(pledge_list
),但通常不会。
控制台还记录以下错误:
Uncaught TypeError: $(...).tab is not a function
这个错误似乎只针对标签2(pledge_list
)触发,据我所知。当console.log
触发时,我的Uncaught TypeError
消息不会触发。
为什么我的.click
函数有时只能捕获?为什么$(this).
没有正确通过?
答案 0 :(得分:2)
jQuery中没有函数tab
:试试这个例子只将你的url放在data-url中:
<div class="container-fluid">
<ul class="nav nav-tabs" id="caseTab">
<li class="case_list" data-url="put your Url"><a href="#">Case List</a></li>
<li class="case_list" data-url="put your Url"><a href="#">Pledge List</a></li>
</ul>
<div class="tab-content">
<div id="case_list">xx</div>
</div>
</div>
<script>
$('.case_list').click(function(){
var url = $(this).data("url");
$('#case_list').load(url);
$(this).addClass('active').siblings().removeClass('active');
});
</script>
答案 1 :(得分:1)
$('.nav-tabs>li>a').click(function(){
var id=$(this).attr('href'), url=$(this).data('url');
$(id).load(url);
});
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<ul class="nav nav-tabs">
<li><a href="#aaa" data-url="put hier Url" data-toggle="tab">Test1</a></li>
<li><a href="#bbb" data-url="put hier Url" data-toggle="tab">Test2</a></li>
<li><a href="#ccc" data-url="put hier Url" data-toggle="tab">Test3</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="aaa">Content Test1</div>
<div class="tab-pane" id="bbb">Content Test2</div>
<div class="tab-pane" id="ccc">Content Test3</div>
</div>
&#13;
答案 2 :(得分:0)
而不是使用Bootstrap,jQuery UI tabs()可能会有帮助,即使使用external page loads也是如此?
$(function() {
$("#tabs").tabs();
});
&#13;
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Tabs - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div id="tabs">
<ul>
<li><a href="#tabs-1">Nunc tincidunt</a></li>
<li><a href="#tabs-2">Proin dolor</a></li>
<li><a href="#tabs-3">Aenean lacinia</a></li>
</ul>
<div id="tabs-1">
<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper
leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum.
Nunc tristique tempus lectus.</p>
</div>
<div id="tabs-2">
<p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean
aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat.
Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
</div>
<div id="tabs-3">
<p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia
nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
<p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit,
magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum
rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
</div>
</div>
</body>
</html>
&#13;