我有一个MVC页面每10秒左右刷新一次,这是使用javascript完成并返回JSON。我的第一个问题是返回时,活动没有正确设置。我已修复此问题,页面甚至返回到刷新前的相同位置。
我的问题是,当我将活动标签设置为显示时,屏幕会闪烁/闪烁,无论您想要调用它。
这就是我在做什么......
VehicleDetails.prototype.requestVehicleDetails = function () {
var self = this;
var active = $('#VehicleTabs li.active').text();
if (active == "Alerts") {
self.activeTab = 0;
}
if (active == "Overview") {
self.activeTab = 1;
}
if (active == "Tires") {
self.activeTab = 2;
}
$.ajax({
type: 'GET',
url: self.vehiclesUrl + "/VehicleDetails?id=" + self.chid + "&alertsPage=" + self.alertsPage + " &warningsPage=" +
self.warningsPage + "&activeTab=" + self.activeTab,
cache: false,
success: function (result) {
self.updateVehicleDetails(result);
$("#data-error-alert").hide();
self = null;
},
error: function (result) {
$("#data-error-alert").show();
self = null;
}
});
};
VehicleDetails.prototype.updateVehicleDetails = function (result) {
var self = this;
self.vehicleDetails = result;
$("#change-status-button").removeAttr("disabled");
$("#change-assignments-button").removeAttr("disabled");
var template = $.templates("#vehicle-deatils-template");
var htmlOutput = template.render(result);
$("#content-placeholder").html(htmlOutput);
//set the active tab back to what it was
this.showActiveTab(this.activeTab);....
VehicleDetails.prototype.showActiveTab = function (activeTab) {
//this does not work to keep from blinking
// on load of the page: switch to the currently selected tab
//var hash = window.location.hash;
//$('#VehicleTabs a[href="' + hash + '"]').tab('show');
if (activeTab === 0) {
$('[href="#tabAlarms"]').tab('show');
}
if (activeTab === 1) {
$('[href="#tabOverview"]').tab('show');
}
if (activeTab === 2) {
$('[href="#tabTires"]').tab('show');
}
};
我尝试过其他解决方案,例如....
<style type="text/css">
.js #VehicleTabs {
display: none;
}
</style>
<script type="text/javascript" src="/scripts/jquery.js"></script>
<script type="text/javascript">
$('html').addClass('js');
</script>
但这只隐藏了标签并显示了第一个标签的数据,但我无法访问其他标签。
非常感谢任何帮助。
答案 0 :(得分:1)
我能够通过从tab div中删除活动类来修复页面刷新时的屏幕闪烁。
<div id="tabAlarms" class="tab-pane fade in">
@*<div id="tabAlarms" class="@(Model.ActiveTab == 0 ? "tab-pane fade in active" : "tab-pane fade in")">*@
<div class="row" id="alerts">
@Html.Partial("_Alerts", Model)
</div>
</div>
<!--Overview tab-->
<div id="tabOverview" class="tab-pane fade in">
@*<div id="tabOverview" class="@(Model.ActiveTab == 1 ? "tab-pane fade in active" : "tab-pane fade in")">*@
<div class="row" id="overview">
@Html.Partial("_Overview", Model)
</div>
</div>
<!--Tires tab-->
<div id="tabTires" class="tab-pane fade in">
@*<div id="tabTires" class="@(Model.ActiveTab == 2 ? "tab-pane fade in active" : "tab-pane fade in")">*@
<div class="row" id="tires">
@Html.Partial("_Tires", Model)
</div>
</div>