使用 getElementById 更改多个 DIV id 的背景
在这个问题上花了几个小时后,我需要把它扔给大众。我在 http://sketch360test.co.uk/test.php 上有一个测试页面我正在尝试使用 onclick 事件同时满足两个目的... 1)显示/隐藏各种结果 div(这是有效的),2)更改“问题”div 的背景图像。默认情况下,所有“问题”div 都有一个绿色背景图像(由 CSS 控制),翻转时有一个黑色图像......我试图让它们每个都有一个黑色图像 onclick,以及重置 bg单击其中一个后,所有其他问题 div 的图像都会变为绿色。希望这是有道理的...如果您查看该页面,应该会更明显!
我已经成功地在单个实例中实现了此功能,即...单击问题 1、问题 2 或问题 3 会将它们变成黑色背景。这是使用内联 onclick 函数调用 css 类来完成的,例如:
<div id="section-menu-1" onclick="this.className='className2';">Question 1</div>
然后我向该元素的容器添加了更多 javascript(问题 1),它设法“重置”或“恢复”黑色背景第3题为绿色。这是使用以下脚本完成的:
<script type="text/javascript">
function changeClass()
{
var e = document.getElementById("section-menu-3");
e.setAttribute('class', 'className1');
e.setAttribute('className', 'className1'); // for IE which does not recognize "class"
return;
}
</script>
请注意,我已经尝试了许多脚本来更改我的 DIV 的类,这是唯一一个一致工作的脚本。但这就是问题所在 - 我想修改上面的脚本以影响所有问题 ID,而不仅仅是“section-menu-3”。顺便说一句,这个演示中只有 3 个问题,但我在最后一页上将有多达 7 或 8 个问题,因此需要修改上面的脚本以便能够获取多个 ID。我已经尝试过像 getElementsByTag 这样的方法,但无法让它们工作(也许是因为我试图将一个类添加到我已经在该过程中应用了一个类的元素)。
我尝试过的任何其他方法最终都会破坏上述迷你脚本的功能。
基本上,帮助! [ 请 :)) ]
having spent hours on this problem i need to throw it out to the masses. I have a test page at http://sketch360test.co.uk/test.php on which I'm trying to use onclick events to serve two purposes at once... 1) to show/hide various results div (this is working), and 2) to change the background image(s) of the 'question' divs. All of the 'question' divs have a green background image by default (controlled by CSS), a black image on rollover... and I'm trying to get them to each have a black image onclick, as well as resetting the bg image for all the OTHER question divs to green when one is clicked. Hope that makes sense... it should be more obvious if you view the page!
I've managed to get this working for a single instance, ie... clicking either Question 1, Question 2 or Question 3 will turn them to a black bg. This has been done using an inline onclick function to call a css class, for example:
<div id="section-menu-1" onclick="this.className='className2';">Question 1</div>
I have then added some more javascript to the container for this element (Question 1), which manages to 'reset' or 'revert' the black bg of Question 3 to green. This has been done using the following script:
<script type="text/javascript">
function changeClass()
{
var e = document.getElementById("section-menu-3");
e.setAttribute('class', 'className1');
e.setAttribute('className', 'className1'); // for IE which does not recognize "class"
return;
}
</script>
Please note here that I have tried NUMEROUS scripts for changing the class of my DIVs, this is the only one that works consistently. Herein lies the problem though - I want to modify the above script to affect all of the Question ID's, not just "section-menu-3". Incidentally, there are only 3 Questions in this demo but I will have up to 7 or 8 on the final page and so need to modify the above script to be able to take numerous ID's. I've tried things like getElementsByTag but haven't been able to get them to work (perhaps because I'm trying to add a class to an element that I've already applied one to for that process).
Anything else I've tried has ended up breaking the functionality of the above mini-script.
Basically, HELP! [ please :)) ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将所有 div 包裹在一个带有 id 的外部 div 中,如下所示:
然后使用您的 javascript 函数:
Wrap all your divs in an outer div with an id like so:
Then using your javascript function:
看看这个演示: fiddle
这应该会让您朝着正确的方向前进。
Take a look at this demo: fiddle
This should put you in the right direction.
基本上,您需要某种通用选择器,正如上面 Paul 所建议的,jquery 是一个很棒的工具。然后您可以为您的问题添加一个班级,例如“myclass”。然后,使用 jquery 执行:
$('.myclass').attr("class", "className1");
或者,您可以直接使用 javascript 来完成: document.getElementsByClassName('myclass')
将返回 DOM 对象数组,您需要迭代每个元素。但是,IE6 SP1 之前的 IE 版本可能不支持 getElementsByClassName。
Basically, you need some kind of generic selector and as Paul above suggests, jquery is a great tool. Then you can add a class to your questions, say "myclass". Then, with jquery you do:
$('.myclass').attr("class", "className1");
Alternatively, you can do it with straight up javascript: document.getElementsByClassName('myclass')
will return an array of DOM objects and you need to iterate over each element. However, getElementsByClassName may not be supported in IE versions before IE6 SP1.
如何实现一个简单的跨浏览器
getElementsByClassName
函数现在使用它您可以简单地执行以下操作:
HTML
脚本
How to implement a simple cross browser
getElementsByClassName
functionNow using this you can simply do the following:
HTML
Script