我有三个“a”选择时会改变颜色的类,但我需要在页面加载时将其设置为默认值
我正在开发这个项目,我需要在列出了三种产品的地方添加此功能。
他们最初是 div 的,但后来将其更改为 ahref 类以连接整个区域。
悬停时该框必须改变颜色 - 我已经做到了。 单击时该框需要更改为另一种颜色 - 我也这样做了。
我不明白的一件事是如何使第二个框默认为选中状态,然后在选择另一个框时关闭颜色
这是我为该页面提供的 javascript。
var highlightLink = function () {
var active = null, colour = '#f6efa2';
if (this.attachEvent) this.attachEvent('onunload', function () {
active = null;
});
return function (element) {
if ((active != element) && element.style) {
if (active) active.style.backgroundColor = '';
element.style.backgroundColor = colour;
active = element;
}
};
}();
这是我认为我需要在 body 标记中添加一个 onload 函数的框之一
<a class="productBox1" href="#" border="0" onclick="highlightLink(this);">
,但我不知道需要什么代码,并且我还需要在选择另一个框时将其取消选中。
任何帮助将不胜感激。
I'm working on this project and I need to add this functionality where we have three products listed.
they started out as div's but changed them to ahref class's to link the entire area.
The box has to change color when hovered - which I have done.
The box needs to change to another color when clicked on - which I have also done.
The one thing I can't figure out is how to make the 2nd box default as selected but then have the color turn off when another one is selected
This is the javascript I have for the page.
var highlightLink = function () {
var active = null, colour = '#f6efa2';
if (this.attachEvent) this.attachEvent('onunload', function () {
active = null;
});
return function (element) {
if ((active != element) && element.style) {
if (active) active.style.backgroundColor = '';
element.style.backgroundColor = colour;
active = element;
}
};
}();
here is one of the boxs
<a class="productBox1" href="#" border="0" onclick="highlightLink(this);">
I'm thinking I need an onload function in the body tag but I don't know what code is needed and I also need it to become unselected when another box is selected.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果每个链接都有它自己的类,请将其用作 ID:
将类用于公共属性。要识别单个元素,请使用 ID。
然后,您可以将其添加到页面底部(结束
标记上方):
或
在
中设置。
If every link has it's own class anyway, use it as ID instead:
Use classes for common properties. For identifying single elements, use IDs.
Then you can add this to the bottom of your page (above the closing
<body>
tag):or set
in the
<head>
.听起来你让事情变得比实际情况更复杂。试试这个(我假设你所有的a标签都有类productBox1):
然后有一个名为highlighted的css类,它有
背景颜色:#f6efa2
。您需要 jQuery 才能使其正常工作。
Sounds like you're making this more complicated than it really is. Try this (I'm assuming all your a tags have class productBox1):
Then have a css class called highlighted which has
background-color: #f6efa2
.You need jQuery in order to make this work properly.