简单的javascript问题-onMouseOver div背景更改
我使用了此代码制作一个导航,其中 onMouseOver 时,相邻 div 的背景会更改为相应的图像。
但是,我修改了代码以反映我需要的多个导航按钮。这可能是我出错的地方。另外,我正在 Sitemaker CMS 的 ColdFusion 环境中工作。这也可能是一个问题。
这是代码。
<script>
function changeStyle1() {
document.getElementById('banners').style.backgroundImage = url('images/banners/contractingbanner.jpg');
}
function changeStyle2() {
document.getElementById('banners').style.backgroundImage = url('images/banners/procurementbanner.jpg');
}
function changeStyle3() {
document.getElementById('banners').style.backgroundImage = url('images/banners/distributionbanner.jpg');
}
function changeStyle4() {
document.getElementById('banners').style.backgroundImage = url('images/banners/printingbanner.jpg');
}
function changeStyle5() {
document.getElementById('banners').style.backgroundImage = url('images/banners/custompacksbanner.jpg');
}
function changeStyle6() {
document.getElementById('banners').style.backgroundImage = url('images/banners/businessdevelopmentbanner.jpg');
}
function changeStyle7() {
document.getElementById('banners').style.backgroundImage = url('images/banners/sustainabilitybanner.jpg');
}
function changeStyleBack() {
document.getElementById('banners').style.backgroundImage = url('images/banners/laundrybanner.jpg');
}</script>
<div id="banners">
</div>
<ul id="nav">
<li><a id="contractingservices" href="body.cfm?id=1" onmouseover="changeStyle1()"; onmouseout="changeStyleBack()">Contracting Services</a></li>
<li><a id="procurement" href="body.cfm?id=1" onmouseover="changeStyle2()"; onmouseout="changeStyleBack()" >Procurement & Data Management</a></li>
<li><a id="distribution" href="body.cfm?id=1" onmouseover="changeStyle3()"; onmouseout="changeStyleBack()">Distribution</a></li>
<li><a id="printing" href="body.cfm?id=1" onmouseover="changeStyle4()"; onmouseout="changeStyleBack()" >Printing</a></li>
<li><a id="laundry" href="body.cfm?id=1">Laundry</a></li>
<li><a id="custompacks" href="body.cfm?id=1" onmouseover="changeStyle5()"; onmouseout="changeStyleBack()" >Custom Packs</a></li>
<li><a id="businessdevelopment" href="body.cfm?id=1" onmouseover="changeStyle6()"; onmouseout="changeStyleBack()" >Business Development</a></li>
<li><a id="sustainability" href="body.cfm?id=1" onmouseover="changeStyle7()"; onmouseout="changeStyleBack()" >Sustainability</a></li>
如何修复此代码以使其可运行或找到另一个 JavaScript 或 jQuery 解决方案?我搜索了又搜索才找到了几十个相同div的JS背景变换器,它们可以很容易地用CSS复制。
I used this code to make a nav in which onMouseOver, the neighboring div's background changes to a corresponding image.
However, I amended the code to reflect the multiple navigation buttons I needed. This is probably where I went wrong. Also, I am working in a ColdFusion environment on Sitemaker CMS. This could also be a problem.
Here's the code.
<script>
function changeStyle1() {
document.getElementById('banners').style.backgroundImage = url('images/banners/contractingbanner.jpg');
}
function changeStyle2() {
document.getElementById('banners').style.backgroundImage = url('images/banners/procurementbanner.jpg');
}
function changeStyle3() {
document.getElementById('banners').style.backgroundImage = url('images/banners/distributionbanner.jpg');
}
function changeStyle4() {
document.getElementById('banners').style.backgroundImage = url('images/banners/printingbanner.jpg');
}
function changeStyle5() {
document.getElementById('banners').style.backgroundImage = url('images/banners/custompacksbanner.jpg');
}
function changeStyle6() {
document.getElementById('banners').style.backgroundImage = url('images/banners/businessdevelopmentbanner.jpg');
}
function changeStyle7() {
document.getElementById('banners').style.backgroundImage = url('images/banners/sustainabilitybanner.jpg');
}
function changeStyleBack() {
document.getElementById('banners').style.backgroundImage = url('images/banners/laundrybanner.jpg');
}</script>
<div id="banners">
</div>
<ul id="nav">
<li><a id="contractingservices" href="body.cfm?id=1" onmouseover="changeStyle1()"; onmouseout="changeStyleBack()">Contracting Services</a></li>
<li><a id="procurement" href="body.cfm?id=1" onmouseover="changeStyle2()"; onmouseout="changeStyleBack()" >Procurement & Data Management</a></li>
<li><a id="distribution" href="body.cfm?id=1" onmouseover="changeStyle3()"; onmouseout="changeStyleBack()">Distribution</a></li>
<li><a id="printing" href="body.cfm?id=1" onmouseover="changeStyle4()"; onmouseout="changeStyleBack()" >Printing</a></li>
<li><a id="laundry" href="body.cfm?id=1">Laundry</a></li>
<li><a id="custompacks" href="body.cfm?id=1" onmouseover="changeStyle5()"; onmouseout="changeStyleBack()" >Custom Packs</a></li>
<li><a id="businessdevelopment" href="body.cfm?id=1" onmouseover="changeStyle6()"; onmouseout="changeStyleBack()" >Business Development</a></li>
<li><a id="sustainability" href="body.cfm?id=1" onmouseover="changeStyle7()"; onmouseout="changeStyleBack()" >Sustainability</a></li>
How do I fix this code to make it operational or find another JavaScript or jQuery solution? I've searched and search only to come up with dozens of same-div JS background changers, which can easily be replicated with CSS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许你可以做这样的事情(例如使用 jQuery):
并更新你的
标签来调用此函数传递预期的样式
Maybe you can do something like this (using jQuery, for example):
And update your
<a />
tags to call this function passing the expected style您应该创建一个简写函数:
然后为每个悬停图像调用它,如下所示:
或者您可以使用 jQuery 的悬停函数,以便不使用 onmouseover 和 onmouseout 两个单独的函数来实现此目的。 此处了解更多相关信息。
You should make one shorthand function:
And then call it for each of the hover images like this:
Or you can use jQuery's hover function so as to not use two separate functions with onmouseover and onmouseout to achieve this. Read more about that here.
你真的不需要写所有这些 JavaScript 行。您只需几行 CSS 代码即可完成您的任务:要更改另一个 DIV 的背景图像,您可以使用 jQuery 代码,如本答案的第 2 部分所示。现在添加:
我希望你明白了。
第 2 部分:使用 jQuery 更改另一个 DIV 的 BG 图像
这就是您真正想要的,对吧?
You really don't need to write all these lines of javascript. You can accomplish your task in just a few lines of CSS code:To change the background image of another DIV, you can use a jQuery code, as in section 2 of this answer.Now add:
I hope you got the idea.
Section 2 : Change BG image of another DIV by usign jQuery
This is what you really wanted, right?