我正在使用 jquery 来构建一个选项卡。我可以在选项卡之间来回切换,并根据我单击的选项卡显示/隐藏内容。
真正需要的是根据单击的选项卡,我需要为每个选项卡调用不同的 javascript。我不想在页面加载时加载所有的 javascirpts。我正在使用 highcharts 来构建图表,这些图表可能非常重。我不想在页面加载时加载所有这些 javascript。我需要根据要单击的选项卡加载 javascript。对我如何轻松完成此任务有任何帮助吗?
现在这就是我正在做的事情:
这是标签的 javascript
<script>
// Wait until the DOM has loaded before querying the document
$(document).ready(function () {
$('ul.tabs').each(function () {
// For each set of tabs, we want to keep track of
// which tab is active and it's associated content
var $active, $content, $links = $(this).find('a');
// If the location.hash matches one of the links, use that as the active tab.
// If no match is found, use the first link as the initial active tab.
$active = $($links.filter('[href="' + location.hash + '"]')[0] || $links[0]);
$active.addClass('active');
$content = $($active.attr('href'));
// Hide the remaining content
$links.not($active).each(function () {
$($(this).attr('href')).hide();
});
// Bind the click event handler
$(this).on('click', 'a', function (e) {
// Make the old tab inactive.
$active.removeClass('active');
$content.hide();
// Update the variables with the new link and content
$active = $(this);
$content = $($(this).attr('href'));
// Make the tab active.
$active.addClass('active');
$content.show();
// Prevent the anchor's default click action
e.preventDefault();
});
});
});
</script>
这是一个示例 highchart javascript,用于创建图表并将其绑定到 div
<script type="text/javascript">
$(document).ready(function() {
$.ajaxSetup({ cache: false });
//$(function() {
function cdc1_web_cpu() {
var timeout;
$.getJSON('cdc1_web_cpu.php', function(data) {
// Create a timer
// Create the chart
$('#web1_cpu').highcharts('StockChart', {
chart: {
borderColor: '#801500',
borderRadius: 20,
borderWidth: 1,
type: 'line',
events: {
load: function(chart) {
this.setTitle(null, {
});
}
},
zoomType: 'x'
}
)};
)};
这是html
<ul class='tabs'>
<li><a href='#tab1'>HOME</a></li>
<li><a href='#tab2'>APPS</a></li>
</ul>
<div id='tab1'>
<div id="container">
<table align="center">
<tr>
<td>
<div id="web1_cpu" class="chart" style="width:550px; height:250px;"></div>
</td>
</tr>
</div>
</div>
<div id='tab2'>
<div id="container">
<table align="center">
<tr>
<td>
<div id="app_cpu" class="chart" style="width:550px; height:250px;"></div>
</td>
</tr>
</div>
</div>