javascript 更改 Z-Index onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6079474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:22:40  来源:igfitidea点击:

Changing Z-Index onclick

javascriptcssz-index

提问by Sean

JavaScript noob here. I'm working on a website and I'm trying to change the z-index of a set of frames using buttons. I can't quite get it to work.

这里是 JavaScript 菜鸟。我正在一个网站上工作,我正在尝试使用按钮更改一组框架的 z-index。我无法让它正常工作。

So far this is what I have.

到目前为止,这就是我所拥有的。

function changeZIndex(i,id) {
  document.getElementById(id).style.zIndex=i;
}

And in the body

而且在体内

<A HREF="#" onclick='changeZIndex(1,'aboutus')'><IMG NAME="one" SRC="button1.bmp"></A>
<A HREF="#" onclick='changeZIndex(1,'contactus')'><IMG NAME="two" SRC="button2.bmp"></A>

Yeah, I realize this is probably the stupidest question ever and the answer is really obvious. This is my first time writing JavaScript though, so please go easy on me! :3

是的,我意识到这可能是有史以来最愚蠢的问题,答案非常明显。这是我第一次编写 JavaScript,所以请放轻松!:3

回答by pthurlow

Make sure your quotes are escaped correctly. try:

确保您的引号正确转义。尝试:

<a href="#" onclick="changeZIndex(1,'aboutus')"><img name="one" src="button1.bmp"></a>
<a href="#" onclick="changeZIndex(1,'contactus')"><img name="two" src="button2.bmp"></a>

note the double quotes around the onclick

注意 onclick 周围的双引号

回答by Ry-

Your quotation marks are wrong - look at the syntax highlighting:

你的引号错了——看语法高亮:

<A HREF="#" onclick='changeZIndex(1,'aboutus')'><IMG NAME="one" SRC="button1.bmp"></A>
<A HREF="#" onclick='changeZIndex(1,'contactus')'><IMG NAME="two" SRC="button2.bmp"></A>

This is how you could do it:

你可以这样做:

<A HREF="#" onclick="changeZIndex(1,'aboutus')"><IMG NAME="one" SRC="button1.bmp"></A>
<A HREF="#" onclick="changeZIndex(1,'contactus')"><IMG NAME="two" SRC="button2.bmp"></A>

回答by bungdito

look at your quotes,
then if you change zindex for 'aboutus' to 1, don't forget to change zindex for 'contactus' to 0.
otherwise if you change zindex for 'contactus' to 1, don't forget to change zindex for 'aboutus' to 0.

看看你的引号,
然后如果你将 'aboutus' 的 zindex 更改为 1,不要忘记将 'contactus' 的 zindex 更改为 0。
否则,如果你将 'contactus' 的 zindex 更改为 1,请不要忘记更改 zindex 'aboutus' 为 0。

<A HREF="#" onclick="changeZIndex(1,'aboutus');changeZIndex(0,'contactus');"><IMG NAME="one" SRC="button1.bmp"></A>
<A HREF="#" onclick="changeZIndex(1,'contactus');changeZIndex(0,'aboutus');"><IMG NAME="two" SRC="button2.bmp"></A>