How can I change a CSS parameter that comes from a 'class' property?

时间:2022-06-23 20:31:19

I'm trying to modify some CSS properties in a given element, this way:

我试图修改给定元素中的一些CSS属性,这样:

var rsc_menu = document.getElementById("rsc_menu");
rsc_menu.style.top = 303;
rsc_menu.style.height = 29;

Unfortunately, after code execution the element still stays in its original position and preserves its original size. I've noticed that alert(rsc_menu.style.top) and alert(rsc_menu.style.height) both show an empty box.
The element is declared like this:

不幸的是,在代码执行后,元素仍然保持在原始位置并保留其原始大小。我注意到警报(rsc_menu.style.top)和警报(rsc_menu.style.height)都显示一个空框。元素声明如下:

<div class="resources_menu" id="rsc_menu">

As you can see, it takes all of its styling parameters from a foreign stylesheet; I defined no style tags for it. May that be the problem? Is there any way to accomplish the task without having to change the <div> tag?

如您所见,它从外部样式表中获取所有样式参数;我没有为它定义样式标签。那可能是问题吗?有没有办法完成任务而无需更改

标签?

1 个解决方案

#1


3  

You have to pass it:

你必须通过它:

  1. As a string
  2. 作为一个字符串

  3. With a valid unit
  4. 有效的单位

So your code would be looking like this:

所以你的代码看起来像这样:

var rsc_menu = document.getElementById("rsc_menu");
rsc_menu.style.top = "303px";
rsc_menu.style.height = "29px";

#1


3  

You have to pass it:

你必须通过它:

  1. As a string
  2. 作为一个字符串

  3. With a valid unit
  4. 有效的单位

So your code would be looking like this:

所以你的代码看起来像这样:

var rsc_menu = document.getElementById("rsc_menu");
rsc_menu.style.top = "303px";
rsc_menu.style.height = "29px";