修改范围内的链接时出现问题

发布于 2024-12-05 13:59:30 字数 2495 浏览 0 评论 0原文

我目前正在学习 javascript/CSS,因此我正在尝试实现动态 Javascript 面包屑:http ://www.webmonkey.com/2010/02/build_dynamic_breadcrumbs_with_javascript/。其代码如下。

我想做的是修改面包屑生成的链接;我希望它们是绿色的阴影,并且在它们不活动或悬停时没有文字装饰。当它们处于活动状态或悬停时,我希望它们变成红色并带有下划线。

面包屑已正确生成,但 CSS 似乎未正确应用。无论如何,这些链接都会带有下划线,并且一开始是蓝色的,被访问后是紫色的。这很奇怪,因为如果我修改所应用的链接的大小、粗细、字体系列等,但不修改颜色或文本装饰。我可能在某个地方犯了初学者的错误,所以提前感谢您的时间和帮助!

我的 CSS 是:

.crumb{
  FONT-WEIGHT: medium;
  FONT-SIZE:medium;
  FONT-STYLE:italic;
  FONT-FAMILY:Arial;
}
.crumb:link, .crumb:visited{
  color: green;
  text-decoration: none;
}
.crumb:hover, .crumb:active: {
  color:red;
  text-decoration: underline;
}

面包屑的代码是:

var crumbsep = " > ";
var precrumb = "<span class=\"crumb\">";
var postcrumb = "</span>";
var sectionsep = "/";
var rootpath = "/"; // Use "/" for root of domain.
var rootname = "Home";

var ucfirst = 1; // if set to 1, makes "directory" default to "Directory"

var objurl = new Object;
objurl['main-default'] = 'Site Home';

// Grab the page's url and break it up into directory pieces
var pageurl = (new String(document.location));
var protocol = pageurl.substring(0, pageurl.indexOf("//") + 2);
pageurl = pageurl.replace(protocol, ""); // remove protocol from pageurl
var rooturl = pageurl.substring(0, pageurl.indexOf(rootpath) + rootpath.length);
if (rooturl.charAt(rooturl.length - 1) == "/") //remove trailing slash
{
  rooturl = rooturl.substring(0, rooturl.length - 1);
}
pageurl = pageurl.replace(rooturl, ""); // remove rooturl fro pageurl
if (pageurl.charAt(0) == '/') // remove beginning slash
{
  pageurl = pageurl.substring(1, pageurl.length);
}

var page_ar = pageurl.split(sectionsep);
var currenturl = protocol + rooturl;
var allbread = precrumb + "<a href=\"" + currenturl + "\">" + rootname + "</a>" +    
postcrumb; // start with root

for (i=0; i < page_ar.length-1; i++)
{
  var displayname = "";
  currenturl += "/" + page_ar[i];
  if (objurl[page_ar[i]])
  {
    displayname = objurl[page_ar[i]];
  }
  else
  {
    if (ucfirst == 1)
    {
      displayname = page_ar[i].charAt(0).toUpperCase() + page_ar[i].substring(1);
    }
    else
    {
      displayname = page_ar[i];
    }
   }
  allbread += crumbsep + precrumb + "<a href=\"" + currenturl + "\">" + displayname + 
    "</a>" + postcrumb;
}
document.write(allbread);

I am currently learning javascript/CSS and so I am trying to implement the Dynamic Javascript Breadcrumbs found at: http://www.webmonkey.com/2010/02/build_dynamic_breadcrumbs_with_javascript/. The code for that is below.

What I am trying to do is modify the links that the breadcrumbs generate; I would like them to be a shade of green and have no text-decoration whenever they are not active or being hovered over. When they are being active or hovered over, I would like them to turn red and become underlined.

The breadcrumbs are being generated correctly, but it seems like the CSS is not being applied correctly. The links are underlined no matter what and are blue to start with, then purple after they are visited. It is weird because if I modify the size, weight, font-family, etc of the links that gets applied, but not the color or text-decoration. I am probably making a beginner's mistake somewhere, so thanks in advance for your time and help!

My CSS is:

.crumb{
  FONT-WEIGHT: medium;
  FONT-SIZE:medium;
  FONT-STYLE:italic;
  FONT-FAMILY:Arial;
}
.crumb:link, .crumb:visited{
  color: green;
  text-decoration: none;
}
.crumb:hover, .crumb:active: {
  color:red;
  text-decoration: underline;
}

The code for the breadcrumbs is:

var crumbsep = " > ";
var precrumb = "<span class=\"crumb\">";
var postcrumb = "</span>";
var sectionsep = "/";
var rootpath = "/"; // Use "/" for root of domain.
var rootname = "Home";

var ucfirst = 1; // if set to 1, makes "directory" default to "Directory"

var objurl = new Object;
objurl['main-default'] = 'Site Home';

// Grab the page's url and break it up into directory pieces
var pageurl = (new String(document.location));
var protocol = pageurl.substring(0, pageurl.indexOf("//") + 2);
pageurl = pageurl.replace(protocol, ""); // remove protocol from pageurl
var rooturl = pageurl.substring(0, pageurl.indexOf(rootpath) + rootpath.length);
if (rooturl.charAt(rooturl.length - 1) == "/") //remove trailing slash
{
  rooturl = rooturl.substring(0, rooturl.length - 1);
}
pageurl = pageurl.replace(rooturl, ""); // remove rooturl fro pageurl
if (pageurl.charAt(0) == '/') // remove beginning slash
{
  pageurl = pageurl.substring(1, pageurl.length);
}

var page_ar = pageurl.split(sectionsep);
var currenturl = protocol + rooturl;
var allbread = precrumb + "<a href=\"" + currenturl + "\">" + rootname + "</a>" +    
postcrumb; // start with root

for (i=0; i < page_ar.length-1; i++)
{
  var displayname = "";
  currenturl += "/" + page_ar[i];
  if (objurl[page_ar[i]])
  {
    displayname = objurl[page_ar[i]];
  }
  else
  {
    if (ucfirst == 1)
    {
      displayname = page_ar[i].charAt(0).toUpperCase() + page_ar[i].substring(1);
    }
    else
    {
      displayname = page_ar[i];
    }
   }
  allbread += crumbsep + precrumb + "<a href=\"" + currenturl + "\">" + displayname + 
    "</a>" + postcrumb;
}
document.write(allbread);

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

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

发布评论

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

评论(4

不必在意 2024-12-12 13:59:30

可能不是一个javascript问题。首先获得正确的标记,然后生成它。 document.write 的 HTML 结果类似于:

<span class="crumb"><a href="...">linkText</a></span>

CSS 应该类似于:

.crumb a {
  font-weight: medium;
  font-size: medium;
  font-style: italic;
  font-family: arial;
}

.crumb a:link, .crumb a:visited {
  color: green;
  text-decoration: none;
}

.crumb a:hover, .crumb a:active {
  color:red;
  text-decoration: underline;
}

.crumb a:active 后有一个额外的冒号,阻止其应用。现在可以工作了。

顺便说一句,为什么要在客户端上使用 document.write 执行此操作,而在服务器上执行此操作并仅发送 HTML 可能要简单得多?

Probably not a javascript question. Firstly get the markup right, then work in generating it. The HTML result of the document.write is something like:

<span class="crumb"><a href="...">linkText</a></span>

The CSS should be something like:

.crumb a {
  font-weight: medium;
  font-size: medium;
  font-style: italic;
  font-family: arial;
}

.crumb a:link, .crumb a:visited {
  color: green;
  text-decoration: none;
}

.crumb a:hover, .crumb a:active {
  color:red;
  text-decoration: underline;
}

There was an extra colon after .crumb a:active preventing it from applying. Works now.

Incidentally, why are you doing this on the client with document.write when it would likely be far simpler to do it on the server and just send the HTML?

尴尬癌患者 2024-12-12 13:59:30

您的 CSS 样式指的是跨度,而不是链接。在任何地方将“.crumb”更改为“.crumb a”,它应该可以正常工作。

祝你好运!

Your CSS styles refer to the spans, not the links. Change '.crumb' to '.crumb a' everywhere and it should work just dandy.

Good luck!

朦胧时间 2024-12-12 13:59:30

如果将 css 更改为:

.crumb a:link, .crumb a:visited{
  color: green;
  text-decoration: none;
}
.crumb a:hover, .crumb a:active {
  color:red;
  text-decoration: underline;
}

因此伪类格式应用于链接而不是跨度,您会得到什么?

活动后额外冒号也。

What do you get if you change the css to:

.crumb a:link, .crumb a:visited{
  color: green;
  text-decoration: none;
}
.crumb a:hover, .crumb a:active {
  color:red;
  text-decoration: underline;
}

so the pseudo class formatting is applied to the link not the span?

Extra colon after the active too.

前事休说 2024-12-12 13:59:30

关于编写 Javascript 的方式:

  1. 在代码顶部组合变量
  2. 使用浏览器检查器运行代码
  3. var objurl = new Object 已弃用,您应该使用文字语法
  4. document.location 返回一个对象,而不是字符串,
  5. 您可以使用 JsLint 来了解更多
  6. 从未有过的 东西使用document.write 最好将代码分配给与

您的代码相关的现有元素,它应该重写为如下内容:

http://jsbin.com/usisix/2/edit#javascript, html,实时

Just about your way to write Javascript:

  1. Combine variables at the top of your code
  2. Use browsers inspector to run your code
  3. var objurl = new Object is deprecated, you should use the literal syntax
  4. document.location returns an object, not a string
  5. you can make the use of JsLint to learn more more things
  6. never use document.write it's better to assign the code to an existing element

regarding your code, it should be re-written to something like:

http://jsbin.com/usisix/2/edit#javascript,html,live

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