Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript div,从java脚本获取css属性_Javascript_Html_Css - Fatal编程技术网

Javascript div,从java脚本获取css属性

Javascript div,从java脚本获取css属性,javascript,html,css,Javascript,Html,Css,嘿,我想从javascript更改一些css属性 但我无法访问该div的css值 以下是我的css文件中的内容 #sidebar { float: left; width: 160px; padding: 25px 10px 0 20px; } #sidebar ul { margin: 0; padding: 0; list-style: none; } 这是我的那个div的html代码 <div id="sidebar"> <%@ include file=

嘿,我想从javascript更改一些css属性

但我无法访问该div的css值

以下是我的css文件中的内容

#sidebar {
float: left;
width: 160px;
padding: 25px 10px 0 20px;
}

#sidebar ul {
margin: 0;
padding: 0;
list-style: none;
}
这是我的那个div的html代码

 <div id="sidebar">
     <%@ include file="some_page.jsp" %>
 </div>
我想更改宽度属性, 你能帮我吗


谢谢

根据此处的代码,尝试以下代码: ​


编辑:

为了给代码提供一个更可读的版本,这更接近quirksmode版本。我改变了它,以消除不必要的变量

var sidebarWidth = getStyle('sidebar','width');

function getStyle(el,styleProp) {
    el = document.getElementById(el);
    var result;
    if(el.currentStyle) {
        result = el.currentStyle[styleProp];
    } else if (window.getComputedStyle) {
        result = document.defaultView.getComputedStyle(el,null)
                                    .getPropertyValue(styleProp);
    } else {
        result = 'unknown';
    }
    return result;
}​

根据此处的代码,尝试以下代码: ​


编辑:

为了给代码提供一个更可读的版本,这更接近quirksmode版本。我改变了它,以消除不必要的变量

var sidebarWidth = getStyle('sidebar','width');

function getStyle(el,styleProp) {
    el = document.getElementById(el);
    var result;
    if(el.currentStyle) {
        result = el.currentStyle[styleProp];
    } else if (window.getComputedStyle) {
        result = document.defaultView.getComputedStyle(el,null)
                                    .getPropertyValue(styleProp);
    } else {
        result = 'unknown';
    }
    return result;
}​

使用firebug和console.log()而不是alert(),您将在firebug的console选项卡中获得更好的消息。使用firebug和console.log()而不是alert(),您将在firebug的console选项卡中获得更好的消息。这用于在未添加内联样式时获取元素的计算样式。要设置样式,可以使用
element.style.width=“100px”
更新其内联样式属性,或者创建样式表并将其附加到文档中。不确定你的具体情况是什么,但是应该有很多答案,这样才能联系起来。好吧,新的例子出于某种原因帮助我编写了setStyle方法,这些方法不会越界,getElementById,有什么想法吗?函数setStyle(elementName,styleProp,styleValue){alert('HERE SET');var el=document.getElementById(elementName);alert(e1);if(el.currentStyle){el.currentStyle[styleProp]=styleValue;}else if(window.getComputedStyle){document.defaultView.getComputedStyle(el,null).setProperty(styleProp,styleValue,'important');}else{alert(“nothing to do”);}}}setStyle('sidebar','width','0px');@shay-如果您试图更新一个元素的样式,只需设置它的
样式。不管是什么
属性。(顺便说一下,在您发布的代码中,第二个
alert()
被指定为
e1
而不是
el
。以下是您的函数:
函数setStyle(elementName,styleProp,styleValue){document.getElementById(elementName).style[styleProp]=styleValue;}setStyle('sidebar','width','0px');​用于在未添加内联样式时获取元素的计算样式。若要设置样式,请使用
element.style.width=“100px”更新其内联样式属性
,或者您创建一个样式表并将其附加到文档中。不确定您的具体情况,但应该有很多答案,以便相互关联。好的,新示例帮助我编写了setStyle方法,因为某些原因,这些方法不会通过行getElementById,有什么想法吗?函数setStyle(elementName,styleProp,styleValue){alert('HERE SET');var el=document.getElementById(elementName);alert(e1);if(el.currentStyle){el.currentStyle[styleProp]=styleValue;}else if(window.getComputedStyle){document.defaultView.getComputedStyle(el,null).setProperty(styleProp,styleValue,'important');}else{alert(“没事做”);}}setStyle('sidebar','width','0px');@shay-如果您试图更新元素的样式,只需设置它的
样式。不管是什么
属性。(顺便说一句,在您发布的代码中,第二个
警报()
e1
而不是
el
。下面是您的函数:
function setStyle(elementName,styleProp,styleValue){document.getElementById(elementName).style[styleProp]=styleValue;}setStyle('sidebar','width','0px');​
var sidebarWidth = getStyle('sidebar','width');

function getStyle(el,styleProp) {
    el = document.getElementById(el);
    var result;
    if(el.currentStyle) {
        result = el.currentStyle[styleProp];
    } else if (window.getComputedStyle) {
        result = document.defaultView.getComputedStyle(el,null)
                                    .getPropertyValue(styleProp);
    } else {
        result = 'unknown';
    }
    return result;
}​