JavaScript、URL 解析、字符串操作、导航

发布于 2024-11-14 19:14:22 字数 2451 浏览 0 评论 0原文

下面是我用 HTML/CSS/Javascript 创建的垂直导航菜单的脚本。您会注意到导航菜单是使用列表(ul 和 li 的)创建的...

我的问题是这样...

我希望每个链接都以粗体显示,并且当它位于该页面上时在其前面显示一个 »。

有了 Javascript,我该怎么做呢?每个页面都有一个与链接非常相​​似的文件夹(例如,子项 1 的路径为 sitename.com/path/sub-item-1)...

我假设我可以以某种方式获取 URL(其中我已经在代码中完成了),以某种方式从链接中获取字符串...然后重新插入该字符串,但在其中添加一个和»。

编辑:我应该提到...此导航是插入到我的所有页面中的包含内容,因此,在每个文档上手动添加粗体和»并不是解决此问题的可行解决方案。

感谢您的帮助!

    <!-- Left Navigation starts here -->
            <!-- if IE -->
            <div id="IE_nav">
            <!-- endif IE -->
            <div id="left_nav">
                <ul>
                    <a href="#"><li id="nav_1" class="nav_button">NAV ITEM 1</li></a>
                                <li class="sub_nav_box" id="sub-nav-1" style="display: none;">
                                    <ul>
                                    <li><a href="#">Sub-Item 1</a></li>
                                    <li><a href="#">Sub-Item 2</a></li>
                                    </ul>
                                </li>
                    <a href="#"><li id="nav_2" class="nav_button">NAV 2</li></a>
                                <li class="sub_nav_box"  id="sub-nav-2" style="display: none;">     
                                    <ul>
                                    <li><a href="#">Sub-Item 1</a></li>
                                    <li><a href="#">Sub-Item 2</a></li>
                                    </ul>
                                </li>
                                    </ul>
            </div></div>

            <script type="text/javascript">

    var URL = window.location.protocol + "://" + window.location.host + "/" + window.location.pathname;

    var pathArray = window.location.pathname.split('/');

if (pathArray[2] == "nav_1")
 {
  document.getElementById("#").style.display = 'block';
  document.getElementById('#').className = 'nav_selected';
 } 

if (pathArray[2] == "nav_2")
{
  document.getElementById("#").style.display = 'block';
  document.getElementById('#').className = 'nav_selected';
}
</script>

您基本上可以忽略到目前为止我所拥有的内容(因为它按照我需要的方式工作),但是我如何在子项链接前面放置一个»并将它们设为粗体...使用javascript。

再次感谢!

Below is the script for a vertical navigation menu I have created in HTML/CSS/Javascript. You will notice the navigation menus are creating using lists (ul & li's)...

My question is this...

I want each link to be bold and display an » in-front of it when it is on that page.

With Javascript, how could I go about this? Each of the pages have a folder very similar to the link (for example, Sub-Item 1 would have a path of sitename.com/path/sub-item-1)...

I assume I can somehow grab the URL (which I already have done in the code), grab the string from the link somehow... and then reinsert the string, but add a and » to it.

EDIT: I SHOULD MENTION... This navigation is an include that plugs into all of my pages, therefore, adding the bold and » by hand on each document is not a feasible solution for this question.

Thanks for your help!

    <!-- Left Navigation starts here -->
            <!-- if IE -->
            <div id="IE_nav">
            <!-- endif IE -->
            <div id="left_nav">
                <ul>
                    <a href="#"><li id="nav_1" class="nav_button">NAV ITEM 1</li></a>
                                <li class="sub_nav_box" id="sub-nav-1" style="display: none;">
                                    <ul>
                                    <li><a href="#">Sub-Item 1</a></li>
                                    <li><a href="#">Sub-Item 2</a></li>
                                    </ul>
                                </li>
                    <a href="#"><li id="nav_2" class="nav_button">NAV 2</li></a>
                                <li class="sub_nav_box"  id="sub-nav-2" style="display: none;">     
                                    <ul>
                                    <li><a href="#">Sub-Item 1</a></li>
                                    <li><a href="#">Sub-Item 2</a></li>
                                    </ul>
                                </li>
                                    </ul>
            </div></div>

            <script type="text/javascript">

    var URL = window.location.protocol + "://" + window.location.host + "/" + window.location.pathname;

    var pathArray = window.location.pathname.split('/');

if (pathArray[2] == "nav_1")
 {
  document.getElementById("#").style.display = 'block';
  document.getElementById('#').className = 'nav_selected';
 } 

if (pathArray[2] == "nav_2")
{
  document.getElementById("#").style.display = 'block';
  document.getElementById('#').className = 'nav_selected';
}
</script>

You can essentially ignore what I have so far (as it works how I need it to), but how can I put a » infront of the Sub-Item links and make them bold... Using javascript.

Thanks again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

感情洁癖 2024-11-21 19:14:22

当每个文件夹中有另一页时。您可能还有每个页面的另一个文件。
那么您可以在每个文件夹中的每个文件中手动将类添加到特定导航项吗?

假设您不使用 PHP 或其他东西在主布局/索引中“包含”页面。

当您使用要包含的内容时(在本例中为导航本身):

您可以用斜杠 / 分隔 URL。
就像你的脚本一样,但那里有一些错误。
这里有更好的东西:

URL:http://site.ext/path/sub-item-1/

var url = window.location.href;
url = url.split('/');
url = url[4];

/*
4 because:
0 = http:
1 = <nothing between the two slashes>
2 = site.ext
3 = path
4 = sub-item-1/
*/

document.getElementById(url).className = 'active';

假设您正在使用 PHP:

您可以添加 PHP 片段每个导航项目并回显其中的类。
那么您将永远不会在客户端遇到 JavaScript 问题。

<li<?php echo ($_GET['page'] == 'sub-item-1' ? ' class="active"' : ''); ?>>Sub Item 1</li>

要将文本加粗并添加 »:

您必须使用 CSS 来执行此操作:

li.active {
    font-weight: bold;
}
li.active:before {
    content: "» ";
}

When you have another page in each folder. You probably have another file for each page too.
So you can add a class to the particular navigation item by hand in each file in each folder?

Assuming your not using PHP or something else to "include" pages in your main layout/index.

When you ARE using something to include (in this case the navigation itself):

You can split the URL by slashes /.
Like in your script right there, but there are some error's in there.
Here something better:

URL: http://site.ext/path/sub-item-1/

var url = window.location.href;
url = url.split('/');
url = url[4];

/*
4 because:
0 = http:
1 = <nothing between the two slashes>
2 = site.ext
3 = path
4 = sub-item-1/
*/

document.getElementById(url).className = 'active';

Say you are using PHP:

You can add a snippet of PHP to each navigation item and echo the class in there.
Then you will never have problems with JavaScript on the client-side.

<li<?php echo ($_GET['page'] == 'sub-item-1' ? ' class="active"' : ''); ?>>Sub Item 1</li>

For making the text bold and adding »:

You'll have to do this with CSS:

li.active {
    font-weight: bold;
}
li.active:before {
    content: "» ";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文