jQuery 的导航问题
我的问题如下:
我有一个简单的导航
<ul class="navigation">
<li class="heading selected">Slider images</li>
<li><a href="index.php?action=staticpages" title="">Static pages</a></li>
<li><a href="index.php?action=accomodation" title="">Accomodation</a></li>
<li><a href="index.php?action=general" title="">General</a></li>
<li><a href="index.php?action=settings" title="">Settings</a></li>
<li><a href="index.php?action=dologout" title="">Logout</a></li>
</ul>
和我的“未完成”jQuery 函数:
$('.navigation li a').click(function(e) {
e.preventDefault();
$('.navigation li').removeClass('heading selected');
$(this).parent().toggleClass('heading selected');
});
类 "heading selected" 的 li 元素是默认/选定的元素。
单击另一个 li 元素后我想要实现的目标:
<li><a href="index.php?action=sliderimages" title="">Slider images</a></li>
<li class="heading selected">Static pages</li>
简而言之,从默认列表类中删除“heading selected”列表类,将该类分配给新单击的元素另外,将锚点 href 添加到默认锚点 href,并从新单击的元素中删除锚点标签。
预先感谢各位! :)
My problem is as follows:
I have a simple navigation
<ul class="navigation">
<li class="heading selected">Slider images</li>
<li><a href="index.php?action=staticpages" title="">Static pages</a></li>
<li><a href="index.php?action=accomodation" title="">Accomodation</a></li>
<li><a href="index.php?action=general" title="">General</a></li>
<li><a href="index.php?action=settings" title="">Settings</a></li>
<li><a href="index.php?action=dologout" title="">Logout</a></li>
</ul>
and my "not finished" jQuery function:
$('.navigation li a').click(function(e) {
e.preventDefault();
$('.navigation li').removeClass('heading selected');
$(this).parent().toggleClass('heading selected');
});
The li element with class "heading selected" is the default/selected element.
What I want to achieve after clicking on the other li element:
<li><a href="index.php?action=sliderimages" title="">Slider images</a></li>
<li class="heading selected">Static pages</li>
in short, remove "heading selected" list class from the default one, assing this class to the newly clicked element, also, add anchor href to the default one, and remove anchor tag from the newly clicked element.
Thanks in advance guys! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了使实现更简单,我建议您不要在
li
中添加/删除a
元素,而只需使用 CSS 将元素样式设置为“默认文本”和 JS 来防止默认行为。您可以使用此作为指南:
JS Fiddle 演示。
请注意,我已将您的“标题选择”类名连接到一个驼峰式的“headingSelected”中,此外,在链接的演示中,我必须猜测第一个链接的 URL 应该是什么。因此,如果您应该尝试直接复制/粘贴,请注意这一点。
Edited to address my misconception that this was used for intra-page navigation, or Ajax interaction.
以下内容似乎可行,但需要包含在需要该功能的所有页面中(无论是内嵌在文档的
head
中还是通过链接的 js 文件):JS Fiddle 演示。
如果您应该使用上述内容,只需确保从
html
中删除headingSelected
类名,并允许脚本在运行时分配该类即可。更简洁,但在其他方面与之前编辑的答案完全相同,您可以使用:
JS Fiddle demo。
参考文献:
click()
。is()
。closest()
。siblings()
。removeClass()
。addClass()
。substring()
。indexOf()
。each()
。text()
。toLowerCase()
。attrubte$="value"
attribute-ends-with 选择器< /a>.To make this somewhat more simple to implement, I'd suggest you don't add/remove the
a
elements from theli
, and simply use CSS to style the elements as 'default text', and JS to prevent the default behaviour.You can use this as a guide:
JS Fiddle demo.
Note that I've concatenated your 'heading selected' class names into one single, camel-cased, 'headingSelected', also, in the linked demo, I had to guess what the URL of the first link should be. So be aware of that if you should try a direct copy/paste.
Edited to address my misconception that this was used for intra-page navigation, or Ajax interaction.
The following seems to work, but will need to be included in all pages that require the functionality (Whether in-line in the
head
of the document or via a linked js file):JS Fiddle demo.
If you should use the above, simply ensure that you remove the
headingSelected
class name from thehtml
, and allow the script to assign the class when it runs.More concisely, but otherwise exactly the same as the previously edited answer, you could use instead:
JS Fiddle demo.
References:
click()
.is()
.closest()
.siblings()
.removeClass()
.addClass()
.substring()
.indexOf()
.each()
.text()
.toLowerCase()
.attrubte$="value"
attribute-ends-with selector.在这里,您有一个解决方案,可以像 David Thomas 提到的那样,从 li 中添加/删除 a 元素。
Here you have a solution that will add/remove the a element from the li as David Thomas mentioned.