修改范围内的链接时出现问题
我目前正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能不是一个javascript问题。首先获得正确的标记,然后生成它。 document.write 的 HTML 结果类似于:
CSS 应该类似于:
.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:
The CSS should be something like:
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?
您的 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!
如果将 css 更改为:
因此伪类格式应用于链接而不是跨度,您会得到什么?
活动后额外冒号也。
What do you get if you change the css to:
so the pseudo class formatting is applied to the link not the span?
Extra colon after the active too.
关于编写 Javascript 的方式:
var objurl = new Object
已弃用,您应该使用文字语法document.location
返回一个对象,而不是字符串,document.write
最好将代码分配给与您的代码相关的现有元素,它应该重写为如下内容:
Just about your way to write Javascript:
var objurl = new Object
is deprecated, you should use the literal syntaxdocument.location
returns an object, not a stringdocument.write
it's better to assign the code to an existing elementregarding your code, it should be re-written to something like: