JS第一页面加载不正确 [英] JS not loading correctly on first pageload

查看:130
本文介绍了JS第一页面加载不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第一次加载页面时,我有一个菜单,菜单中的元素可能有current_page类,通过 PHP 添加如果当前页是菜单元素。现在的问题是当我第一次进入页面或刷新它与 ctrl + f5 ,这是相同的,假设走在current_page 课稍稍偏离。不同的Firefox和Chrome(见下面的图片),但在我刷新页面与正常的f5或点击一些链接,所以页面重新加载,一切都向右转,工作,直到我重新加载页面。我试图使一个JS小提琴,但有它的工作每次。所以我会在网上发布的链接,所以你可以看到它,告诉我,如果你也有问题。

 < nav> 
< div class =menu_slider>< / div> < a href =index.php>< div class =navigation current_page>首页< / div>< / a>
< a href =about_us.php>< div class =navigation>关于我们< / div>< / a>
< a href =#target =_ blank>< div class =navigation>博客< / div>< / a>
< a href =magazine.php>< div class =navigation>杂志< / div>< / a>
< a href =contact.php>< div class =navigation>与我联系< / div>< / a>
< / nav>

我必须粘贴一些代码,否则不会让我发表评论。


解决方案

使用jquery或将代码放入a window.onload 方法。这将最有可能帮助它。请让我知道。


When I load the page for the first time, I have a menu, element in the menu might have "current_page" class which I add via PHP if the current page is the menu element. Now the problem is when I enter the page for the first time or refresh it with ctrl+f5, which is the same, the slider that is suppose to go under the "current_page" class is slightly off. Different in Firefox and Chrome (see pictures below), but after I refresh the page with normal f5 or click on some links, so the page reloads, everything goes bask to the right and works until I hardreload the page again. I tried to make a JS fiddle but there it works every time. So I will post the link online, so you can see it there and tell me if you also have the problem.

<nav>
    <div class="menu_slider"></div> <a href="index.php"><div class="navigation current_page">Home</div></a>
    <a href="about_us.php"><div class="navigation">About Us</div></a>
    <a href="#" target="_blank"><div class="navigation">Blog</div></a>
    <a href="magazine.php"><div class="navigation">Magazine</div></a>
    <a href="contact.php"><div class="navigation">Contact me</div></a>
</nav>

I have to paste some code or it won't let me post the comment.

Thank you for any help possible.

解决方案

Use jquery or put your code into a window.onload method. That will most probable help it. Please let me know.

这篇关于JS第一页面加载不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆