脚本不适用于“单击”和“选定”导航状态
尝试让我的导航具有单击和选定状态,但我无法使用此代码执行此操作(网站是: http ://bit.ly/rgwsite )
$('nav li a').click(function() {
$(this).parent().addClass('on').siblings().removeClass('on');
});
nav li 如下
<nav>
<li class="highlt">
<a href="index.php" class="home"><span>Home</span></a>
</li>
我们需要使用 jquery/javascript 操作将类添加到导航中的原因是因为它在新页面加载时不会刷新。例如,当您在主页上单击“体验 RGW”选项卡时,它只会加载标题下方该页面的内容(在“#ajax”div 内)。目前,这些脚本都不起作用。他们没有理由不应该...是否还有其他原因导致页面无法识别 jquery 脚本并单击运行它?我问的主要原因是因为我尝试测试该功能并添加警报,但即使这样也不起作用
提前致谢!
Trying to have my navigation have an on click and selected state, but I am not able to do so with this code (website is: http://bit.ly/rgwsite )
$('nav li a').click(function() {
$(this).parent().addClass('on').siblings().removeClass('on');
});
nav li is as follows
<nav>
<li class="highlt">
<a href="index.php" class="home"><span>Home</span></a>
</li>
The reason we need to use a jquery/javascript action to add the class to the navigation is because it doesn't refresh when a new page loads. For instance, when you're on the home page and click on the tab "Experience RGW", it only loads the content for that page below the header (within the "#ajax" div). Currently, none of these scripts are working. There is no reason they shouldn't... could there be something else causing the page not to recognize the jquery script and run it on-click? The main reason I ask is because I've tried to test the function and add an alert, but even that didn't work
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
siblings 已选择您当前的元素,您不想删除他的“on”类。
编辑:nvm,你的代码工作正常:看看这个jsfiddle。
重新编辑:我完全错了, siblings 不包含当前元素。
re-re-edit :如果您链接到另一个页面 (
href="index.php"
),该页面将被重新加载(或将加载一个新页面),并且您的 JavaScript 点击操作将被忘记了,难道是错误?siblings has your current element selected, you don't want to remove his 'on' class.
edit : nvm, your code works fine : look at this jsfiddle.
re-edit : I'm totally wrong, siblings does not contain the current element.
re-re-edit : If you link to another page (
href="index.php"
), the page will be reloaded (or a new one will be loaded) and your JavaScript click action will be forgotten, could it be the error?您可能想这样尝试:
您所做的是将类“on”添加到 LI,然后使用
.siblings().removeClass('on'); 删除它
You might wanna try it like this:
What you did is add the class "on" to the LI and then removed it with
.siblings().removeClass('on');