简体   繁体   English

在JavaScript文件中更改CSS类元素的高度

[英]Change css class element height in a javascript file

Dear Fellow programmers 亲爱的资深程序员

I am working on a project, that has minimized versions of amChart JavaScript files in it. 我正在做一个项目,该项目中的amChart JavaScript文件版本已降至最低。 The problem is I need to change a height of a specific class element in those JS files. 问题是我需要更改那些JS文件中特定类元素的高度。 However, they are minimized. 但是,它们被最小化。 So I cant really change anything about them. 所以我真的不能改变他们的任何事情。

I got an idea of crating a new JS file, change the height attribute of that class, and place it in the resources page under the other amchart minimized JavaScript files, that way it could read my JS at the end and place the height attribute that I need, since I believe the way it works is it reads the JS files in order. 我想到了创建一个新的JS文件,更改该类的height属性并将其放置在另一个amchart最小化的JavaScript文件下的资源页面中的想法,这样它就可以读取我的JS并将其高度属性我需要,因为我相信它的工作方式是按顺序读取JS文件。

In case you are wondering why don't I just make a CSS file instead of a JavaScript file is because JS files are read after CSS files so it does not matter where I place it. 如果您想知道为什么我不只是制作一个CSS文件而不是一个JavaScript文件是因为JS文件是在CSS文件之后读取的,所以我放置它的位置无关紧要。

Can you tell me if that is how we change a height attribute for a class element in a JavaScript file ? 您能告诉我这是否是我们如何更改JavaScript文件中类元素的height属性吗? the class is .amchartsLegend 该类是.amchartsLegend

//changing the height attribute of amChartsLegend

var myElements = document.querySelectorAll(".amChartsLegend")
myElement.style.height("20px")

One more question is should document be replaced by the actual name of the amchart minimized JS file ? 还有一个问题是,应使用amchart最小化JS文件的实际名称替换文档吗? assuming that's the correct code for changing the height 假设这是更改高度的正确代码

Use the for statement since you are using the document.querySelectorAll it returns an array 由于您正在使用document.querySelectorAll,因此使用for语句 ,它将返回一个数组

var myElements = document.querySelectorAll(".amChartsLegend");
console.log(myElements.length);
for (var i= 0; i < myElements.length ; i++) { 
   myElements[i].style.height = "20px"
}

here is an example 这是一个例子

 var myElements = document.querySelectorAll(".amChartsLegend"); console.log(myElements.length); for (var i= 0; i < myElements.length ; i++) { myElements[i].style.height = "20px" } 
 memu li{ background: #ccc; } 
 <menu> <li class=amChartsLegend>amChartsLegend</li> <li>normal</li> <li class=amChartsLegend>amChartsLegend</li> <li class=amChartsLegend>amChartsLegend</li> <li>normal</li> <li>normal</li> </menu> 

在此处输入图片说明

Instead of using Javascript, why don't you override the style instead? 除了使用Javascript之外,为什么不覆盖样式呢?

<style type='text/css'>
    .amChartsLegend {
        height: 20px !important;
    }
</style>
var nodes = document.querySelectorAll(".amChartsLegend")
for (var i =0; i < nodes.length; i++) {
    nodes[i].style.height = "20px";
}

you should use 你应该使用

var myElements = document.querySelectorAll(".amChartsLegend");
for (var i= 0; i < myElements.lenght ; i++) { 
myElement[i].style.height="20px";
}

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

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