我希望首先运行该函数,然后在完整页面完全加载时运行addClass()方法。现在看来它们似乎是在同一时间执行的。有没有办法设定某种优先权?
$(window).load(function() {
// Priority 1
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
// Priority 2
$("a[data-page='0']").addClass("selected-page");
});
答案 0 :(得分:0)
$(window).load(function() {
// Priority 1
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
document.write([year, month, day].join('-'));
}
formatDate(new Date().getMilliseconds());
// Priority 2
$("a[data-page='0']").addClass("selected-page");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>