开发者

how can I set bottom in javascript

开发者 https://www.devze.com 2023-03-18 04:52 出处:网络
I would like to know if this is correct to set the bottom css using javascript: var productElement2 = document.getElementsByTagName(\"footer\");

I would like to know if this is correct to set the bottom css using javascript:

var productElement2 = document.getElementsByTagName("footer");
productElement2.style.bottom=="0"

I ask this because my script does not work :p. Thanks for the feedback :). If this is correct something else might be wrong but I开发者_运维技巧 just want to be sure.


Use the = operator for assignment. The == operator is for comparison.

productElement2.style.bottom = "0";

Note: If the value is a non-zero value, it needs a unit. Example:

productElement2.style.bottom = "10px";

Also, as kinakuta pointed out, the getElementsByTagName returns an array of elements, so you have to get one elements from the array. If you only have one footer, just get the first element:

var productElement2 = document.getElementsByTagName("footer")[0];


Be sure you select the specific footer in the returned array you want to style:

var productElement2 = document.getElementsByTagName("footer")[0];


Just use one equal sign. Two is for comparison.

productElement2.style.bottom = "0";


This is mostly a CSS question. The CSS style bottom: 0 will not work when position is "static" (the default in-layout position). You would have to specify something like this CSS (with position of absolute or fixed):

#footer {
    position: absolute;
    bottom: 0;
}

or this code:

var productElement2 = document.getElementById("footer");
productElement2.style.position = "absolute";
productElement2.style.bottom = "0";

Also, note the single equals sign to set a variable. You had a double equals sign which is a test of equality, not an assignment.

In addition, your getElementsByTagName returns an array, not a single item. If you wanted to get a single item with a CSS id, you would use getElementById as I've shown here. If you wanted to get the first item from the array returned by getElementsByTagName, then you'd have to reference the first item in the array rather than the entire array.


footer should have position:fixed defined. Try this: http://jsfiddle.net/SSkqb/

0

精彩评论

暂无评论...
验证码 换一张
取 消