Updating z-index with javascript invalid left-hand side in assignment

可紊 提交于 2019-12-20 01:57:27

问题


I'm having a problem with Javascript. It's almost certainly something I'm doing wrong as I'm quite new to js. I'm trying to modify a div tag's z-index to 1 from -1 so an image contained in it appears in front of the other images, but keep on getting "invalid left-hand side in assignment" as an error. I've tried searching the net, but could not find a solution, probably because I'm making a rudimentary mistake. Snippets of my code are as follows:

JavaScript:

function bubble1()
{       
    document.getElementById("img-bubble1").style.z-index = "1";//this is the line it crashes on     
    bubble2move = setInterval(function() {bubble2()}, 2000);
}   

html:

<td width="50%">
    <div class='image-window'>
        <div id="img-bubble1" class='img img-bubble hide'><img src="../img/img-bubble1.gif"/></div>
        <div id="img-bubble2" class='img img-bubble hide'><img src="../img/img-bubble2.gif"/></div>             
    </div>
</td>

CSS:

.image-window
{
    width: 435px;
    height: 400px;
    float: left;
    position: relative;
    overflow: hidden;
    padding: 0;
    margin: 0;
}

#img-bubble1, #img-bubble2
{
    position: absolute;
    z-index: -1;
}

.img-bubble
{
    width: 206.25px;
    height: 400px;
    position: absolute;
    top: 0;
    right: 0;
}

Thanks in advance, and sorry again for my inexperience.


回答1:


When addressing CSS properties in javascript, anything that has a hyphen is transformed to camel case. Try this instead:

document.getElementById("img-bubble1").style.zIndex = "1";

Related Q/A

Edit: Additional information for posterity, as said by khanahk in a comment, a hyphen in a variable name will be interpreted as a minus sign, and this is not valid assignment syntax. An interesting and not recommended way around this is to use bracket notation instead:

document.getElementById("img-bubble1").style["z-index"] = "1";


来源:https://stackoverflow.com/questions/13588733/updating-z-index-with-javascript-invalid-left-hand-side-in-assignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!