jQuery 如何根据变量显示不同的项目

发布于 2024-07-25 23:03:27 字数 2741 浏览 0 评论 0原文

我想创建一个可以在每个页面上使用的导航栏。 大多数导航项具有相同的样式,但是与当前页面相关的导航项具有不同的样式。 我希望能够使用 jQuery 读取元标记并显示正确的样式。

我已将代码粘贴在下面 - 这似乎不起作用:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="page-name" content="about"/>
<title>Untitled Document</title>
<link href="frame_styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.js" ></script>
<script>
var pageName = $('meta[name=page-name]').attr("content");
    $(document).ready(function() {
        if (pageName="about") {
            $('#about').addClass('selected');
      }
    });
</script>
</head>

<body>

<table border="0" cellspacing="0" cellpadding="8">
  <tr>
<td align="center" bgcolor="#0099CC">&nbsp;</td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks"href="">Home</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks" href="">News &amp; Events</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a id="about" class="navLinks" href="">About</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks" href="">Services</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks" href="">eResearch</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks" href="">Industry &amp; Government</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC" class="selected">Education &amp; Training</td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a href=""  class="navLinks" >Multimedia</a></td>
    <td align="center" nowrap="nowrap" bgcolor="#0099CC">&nbsp;</td>
  </tr>
</table>
<p>s</p>
</body>
</html>`



@charset "utf-8";
/* CSS Document */

.navLinks{
font-family:Verdana, Geneva, sans-serif;
font-size:small;
color:#FFF;
text-decoration:none;
}

.quick-links{
    text-decoration:none;
}

#quick-links-table{
    border-top:2px solid #0099CC;
    border-bottom:1px solid #0099CC;
}

.text{
    font-family:Verdana, Geneva, sans-serif;
    font-size:small;
}

.selected{
    background:#FFF;
    font-family:Verdana, Geneva, sans-serif;
    font-size:small;
    color:#000;
}

I want to create a navigation bar that cna be used on every page. Most nav items have the same styling, however the nav item that relates to the current page has differen tstyleing. I want to be able to use jQuery to read a meta tag and display the correct styling.

I have pasted my code below - this doesn't seem to work:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="page-name" content="about"/>
<title>Untitled Document</title>
<link href="frame_styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.js" ></script>
<script>
var pageName = $('meta[name=page-name]').attr("content");
    $(document).ready(function() {
        if (pageName="about") {
            $('#about').addClass('selected');
      }
    });
</script>
</head>

<body>

<table border="0" cellspacing="0" cellpadding="8">
  <tr>
<td align="center" bgcolor="#0099CC"> </td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks"href="">Home</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks" href="">News & Events</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a id="about" class="navLinks" href="">About</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks" href="">Services</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks" href="">eResearch</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a class="navLinks" href="">Industry & Government</a></td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC" class="selected">Education & Training</td>
<td align="center" nowrap="nowrap" bgcolor="#0099CC"><a href=""  class="navLinks" >Multimedia</a></td>
    <td align="center" nowrap="nowrap" bgcolor="#0099CC"> </td>
  </tr>
</table>
<p>s</p>
</body>
</html>`



@charset "utf-8";
/* CSS Document */

.navLinks{
font-family:Verdana, Geneva, sans-serif;
font-size:small;
color:#FFF;
text-decoration:none;
}

.quick-links{
    text-decoration:none;
}

#quick-links-table{
    border-top:2px solid #0099CC;
    border-bottom:1px solid #0099CC;
}

.text{
    font-family:Verdana, Geneva, sans-serif;
    font-size:small;
}

.selected{
    background:#FFF;
    font-family:Verdana, Geneva, sans-serif;
    font-size:small;
    color:#000;
}

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

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

发布评论

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

评论(2

扭转时空 2024-08-01 23:03:29

怎么样:

var meta = $("meta").get(1).attr("content");
if(meta == 'about') {
    $('#about').addClass('selected');
}

How about:

var meta = $("meta").get(1).attr("content");
if(meta == 'about') {
    $('#about').addClass('selected');
}
锦欢 2024-08-01 23:03:28

@Ankur它正在工作,但是更改此行:

if(pageName="about")

您可以使用 pageName来

if(pageName=="about")

代替比较,如下所示:

$("#"+pageName).addClass("selected");

@Ankur It is working, but change this line:

if(pageName="about")

to

if(pageName=="about")

instead of comparison you can use pageName like this:

$("#"+pageName).addClass("selected");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文