jQuery 的导航问题

发布于 2024-12-08 07:11:29 字数 1427 浏览 0 评论 0原文

我的问题如下:

我有一个简单的导航

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2024-12-15 07:11:29

为了使实现更简单,我建议您不要在 li 中添加/删除 a 元素,而只需使用 CSS 将元素样式设置为“默认文本”和 JS 来防止默认行为。

您可以使用此作为指南:

$('.navigation li a').click(
    function(){
        if ($(this).is('.headingSelected')) {
            return false;
        }
        else {
            $(this)
                .closest('li')
                .siblings()
                .removeClass('headingSelected');
            $(this).closest('li').addClass('headingSelected');
            return false;
        }
    });

JS Fiddle 演示

请注意,我已将您的“标题选择”类名连接到一个驼峰式的“headingSelected”中,此外,在链接的演示中,我必须猜测第一个链接的 URL 应该是什么。因此,如果您应该尝试直接复制/粘贴,请注意这一点。


Edited to address my misconception that this was used for intra-page navigation, or Ajax interaction.

以下内容似乎可行,但需要包含在需要该功能的所有页面中(无论是内嵌在文档的 head 中还是通过链接的 js 文件):

var currentURL = "http://www.yourwebsite.com/index.php?action=staticpages";
var page = currentURL.substring(currentURL.indexOf('=') + 1);

$('.navigation a').each(
    function(i){
        if ($(this).text().toLowerCase().replace(' ','') == page) {
            $(this).closest('li').addClass('headingSelected');
        }
    });

JS Fiddle 演示

如果您应该使用上述内容,只需确保从 html 中删除 headingSelected 类名,并允许脚本在运行时分配该类即可。

更简洁,但在其他方面与之前编辑的答案完全相同,您可以使用:

var currentURL = "http://www.yourwebsite.com/index.php?action=staticpages";
var page = currentURL.substring(currentURL.indexOf('=') + 1);

$('a[href$="' + page + '"]').closest('li').addClass('headingSelected');

JS Fiddle demo。

参考文献:

To make this somewhat more simple to implement, I'd suggest you don't add/remove the a elements from the li, 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:

$('.navigation li a').click(
    function(){
        if ($(this).is('.headingSelected')) {
            return false;
        }
        else {
            $(this)
                .closest('li')
                .siblings()
                .removeClass('headingSelected');
            $(this).closest('li').addClass('headingSelected');
            return false;
        }
    });

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):

var currentURL = "http://www.yourwebsite.com/index.php?action=staticpages";
var page = currentURL.substring(currentURL.indexOf('=') + 1);

$('.navigation a').each(
    function(i){
        if ($(this).text().toLowerCase().replace(' ','') == page) {
            $(this).closest('li').addClass('headingSelected');
        }
    });

JS Fiddle demo.

If you should use the above, simply ensure that you remove the headingSelected class name from the html, 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:

var currentURL = "http://www.yourwebsite.com/index.php?action=staticpages";
var page = currentURL.substring(currentURL.indexOf('=') + 1);

$('a[href$="' + page + '"]').closest('li').addClass('headingSelected');

JS Fiddle demo.

References:

银河中√捞星星 2024-12-15 07:11:29

在这里,您有一个解决方案,可以像 David Thomas 提到的那样,从 li 中添加/删除 a 元素。

$('.navigation li a').click(function (e) {
    e.preventDefault();
    $('.navigation li').removeClass('heading selected');
    $('.navigation li').each(function (index) {
        if ($(this).children().size() == 0) {
            $(this).html("<a href='index.php?action=" + $(this).text().replace(' ', '').toLowerCase() + "'>" + $(this).text() + "</a>")
        }
    });
    $(this).parent().html($(this).text());
    $(this).parent().toggleClass('heading selected');
});

Here you have a solution that will add/remove the a element from the li as David Thomas mentioned.

$('.navigation li a').click(function (e) {
    e.preventDefault();
    $('.navigation li').removeClass('heading selected');
    $('.navigation li').each(function (index) {
        if ($(this).children().size() == 0) {
            $(this).html("<a href='index.php?action=" + $(this).text().replace(' ', '').toLowerCase() + "'>" + $(this).text() + "</a>")
        }
    });
    $(this).parent().html($(this).text());
    $(this).parent().toggleClass('heading selected');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文