简体   繁体   English

(不能)在 javascript 中获取元素样式的一部分

[英](can't) Get part of an element's style in javascript

I'm very new to javascript (started messing with it today).我对 javascript 很陌生(今天开始搞砸了)。

I'm trying to change the height of an element (a div) called 'bar'.我正在尝试更改名为“bar”的元素(div)的高度。 The bar will be part of a chart.条形图将成为图表的一部分。

I have no problem hooking up a button to a function which changes the height of the bar.我可以将按钮连接到更改栏高度的功能上。 All is working fine, except the bar is growing in the downward direction instead of upward like i wanted.一切正常,除了酒吧是向下生长而不是像我想要的那样向上生长。

My solution is to increase the 'height' property, and then decrease the 'top' property by the height increase amount.我的解决方案是增加 'height' 属性,然后通过高度增加量减少 'top' 属性。

So I want to do something like:所以我想做一些类似的事情:

bar.style.height = newHeight;
bar.style.top = bar.style.top - newHeight;

And that should increase the height upward.这应该向上增加高度。 The problem is that I don't know how to get the current value of bar.style.top问题是我不知道如何获取 bar.style.top 的当前值

In firebug, if I look at the styles in the html tab, the bar has a top of在萤火虫中,如果我查看 html 选项卡中的样式,则该栏的顶部为

top: 122px;

but in the watch list for bar, all i see is the value "" (empty string) for bar.style.top.但是在 bar 的监视列表中,我看到的只是 bar.style.top 的值“”(空字符串)。 I guess my question is - why don't these values match, and how can i get to the value i want?我想我的问题是 - 为什么这些值不匹配,我怎样才能达到我想要的值?

And one other thing while I'm asking about this stuff:当我询问这些东西时,还有一件事:

I have some CSS for webkit that looks like:我有一些用于 webkit 的 CSS,如下所示:

-webkit-animation-timing-function: ease-in-out;

for example.例如。 I notice in the watch list for bar, there is no bar.style.-webkit-animation-timeing-function.我注意到在 bar 的监视列表中,没有 bar.style.-webkit-animation-timeing-function。 How do I get at and change values like that?我如何获得和改变这样的价值观?

I normally use asp.net, I was hoping there was something similar to我通常使用asp.net,我希望有类似的东西

bar.Style["property"] = "value";

Have I missed something here?我在这里错过了什么吗?

  1. Please use document.getElementById('bar') instead of just bar , because bar could mean a lot of different things.请使用document.getElementById('bar')而不是bar ,因为bar可能意味着很多不同的东西。
  2. The style property allows you to override any style but only gives you style set by a <div style="..."> attribute. style属性允许您覆盖任何样式,但只为您提供由<div style="...">属性设置的<div style="..."> See http://www.quirksmode.org/dom/getstyles.html on how to get the current style.有关如何获取当前样式的信息,请参阅http://www.quirksmode.org/dom/getstyles.html

Use bar.style.WebkitAnimationTimingFunction to access the CSS property via JavaScript.使用bar.style.WebkitAnimationTimingFunction通过 JavaScript 访问 CSS 属性。

http://www.codestyle.org/javascript/dom/css/CSS3ExtensionStyles.shtml http://www.codestyle.org/javascript/dom/css/CSS3ExtensionStyles.shtml

Since you're going for a bar chart, I would suggest styling a bit differently using "bottom" instead of "top".由于您要使用条形图,我建议使用“底部”而不是“顶部”来设计不同的样式。 Then, you just have to change the height.然后,您只需要更改高度。 Something like the following:类似于以下内容:

<div style="position: relative; width: 500px; height: 300px; border: 1px solid black;">
    <div style="position: absolute; bottom: 10px; left: 10px; width: 20px; height: 40px; background-color: red;"></div>
    <div style="position: absolute; bottom: 10px; left: 40px; width: 20px; height: 60px; background-color: blue;"></div>
    <div style="position: absolute; bottom: 10px; left: 70px; width: 20px; height: 80px; background-color: green;"></div>
</div>

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

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