简体   繁体   English

使用javascript更改CSS样式

[英]change css style using javascript

hi in my php application i want to change the style using javascript 您好在我的PHP应用程序中,我想使用javascript更改样式

<div id="middle-cnt" class="ad-image-wrapper"><div class="ad-image" style="width: 1000px; height: 450px; top: 0px; left: 131px;"><img height="450" width="1000" src="images/Gallery2/4.jpg"><p class="ad-image-description" style="width: 1000px; bottom: 0px;"><table width="100%"><tbody><tr><td class="image-desc">Stevie III &ndash; 36" long x 24" wide - Oil on Canvas<div style="color: rgb(187, 167, 0); padding-left: 10px; position: absolute; top: 0pt; left: 450px; font-size: 35px; letter-spacing: 6px;">SOLD</div></td></tr></tbody></table></p></div></div>

In the baove code i want to change the div style left:0px ie <div id="middle-cnt" class="ad-image-wrapper" ><div class="ad-image" style="width: 1000px; height: 450px; top: 0px; left: 0px;"><img .. 在baove代码中,我想将div样式更改为左:0px,即<div id="middle-cnt" class="ad-image-wrapper" ><div class="ad-image" style="width: 1000px; height: 450px; top: 0px; left: 0px;"><img ..

I am using the script below. 我正在使用以下脚本。

function fnshowgallery(){
var elm=document.getElementById('middle-cnt').childNodes;

    var divElm=elm[0];
    divElm.style.Left='0px';


}

But it's not get the desired result. 但却没有得到理想的结果。

Does any one know this? 有人知道吗?

But i don't know how. 但是我不知道如何。

document.getElementById("middle-cnt").childNodes[0].style.left = "0px";

这应该可以,如果不能,请仔细检查您的DOM结构。

如果您希望属性起作用,则元素也需要位置属性。

javascript is a case sensitive language. javascript是区分大小写的语言。 and it's camel case. 这是骆驼案。 So it's not Left but **left. 所以它不是左,而是**左。 It has to be: 它一定要是:

divElm.style.left='0px';  

If I evaluate that javascript in my console with that HTML snippet, it works; 如果我在控制台中使用该HTML代码段评估了该javascript,则可以正常运行; the "left" styling is changed to 0px. “左”样式更改为0px。

But , I think you need to have the parent (#middle-cnt) to have position:relative , and your div (.ad-image) with position:absolute . 但是 ,我认为您需要让父项(#middle-cnt)具有position:relative ,而您的div(.ad-image)则具有position:absolute Maybe you have this in your stylesheet... 也许您的样式表中有这个...

Might help if you say what browser(s) you have tested with, as well. 如果您还说说您使用过哪种浏览器,也可能会有所帮助。

Rather than manipulate the CSS directly with JavaScript it's usually a better approach to only use JavaScript to set the CSS class name and leave all your CSS in your .css files. 而不是操纵CSS用JavaScript直接它通常是一个更好的办法, 使用JavaScript设置CSS类名,并留下您的所有CSS在你的.css文件。 For example: 例如:

/* CSS */
.foo {
    height:450px;
    left:0;
    position:relative; /* or maybe absolute? */
    top:0;
    width:1000px;
}

Then change your JavaScript to something like: 然后将JavaScript更改为:

var elm = document.getElementById('middle-cnt').childNodes[0];
if (elm) {
    var cssClass = (elm.className) ? ' foo' : 'foo';
    elm.className += cssClass;
}

Then, if you need to make any more changes to your CSS you shouldn't need to touch the Javascript. 然后,如果您需要对CSS进行更多更改,则无需触摸Javascript。

But for the left property to work in your CSS you'll need to set the position property to either relative or absolute . 但是left属性在CSS中起作用,您需要将position属性设置为relativeabsolute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM