如何从javascript文件中获取div的样式

如何从javascript文件中获取div的样式,javascript,jquery,css,styles,Javascript,Jquery,Css,Styles,我是javascript新手,我在一个js文件中使用javascript为div提供了样式,我想从另一个js文件中获得该样式。这怎么可能?? 当我使用 var height=$(“#searchComment”).css(“height”) 在警告结果时,它会变成“未定义”。 如果此样式以html形式给出,它将正确返回。 test1.js和test2.js包含在index.html中 在test1.js中,我为id为“searchComment”的div提供了样式 $( "#parentDiv"

我是javascript新手,我在一个js文件中使用javascript为div提供了样式,我想从另一个js文件中获得该样式。这怎么可能?? 当我使用
var height=$(“#searchComment”).css(“height”)
在警告结果时,它会变成“未定义”。
如果此样式以html形式给出,它将正确返回。
test1.js和test2.js包含在index.html中
在test1.js中,我为id为“searchComment”的div提供了样式

$( "#parentDiv").append("<div class='ui-li-desc' id='searchComment' style='height:50px; width:40 px'></div>");

and in another js file test2.js i want to get the style of div of id 'searchComment'.
how can i get this style?? please help me.
Thank you
$(“#parentDiv”)。追加(“”);
在另一个js文件test2.js中,我想获得id为'searchComment'的div的样式。
我怎样才能得到这种款式??请帮帮我。
非常感谢。

您可以从任何文件更改任何元素的样式,但标准做法是添加类而不是更改元素的样式。要添加类,可以使用.addClass('new-class')jQuery函数。并将新类的所有样式放在单独的CSS文件中。如果您只想添加样式而不关心标准,那么您可以使用.css jquery函数

$(".number").css({
  'attribute1': value,
  'attribute2': value,
});

使用jquery选择器,然后更改css:

   $('.number').css({'font-size': '12px', 'text-align': 'left'});

您必须使用可以从其他JS获得的ID来为该div提供ID

限制:

Both js should be using in that HTML file.

Before using id of <div> you have to create that div 

我想您需要css类使用的描述。转介

function getStyleRules(className) {
    var class = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
    for (var x = 0; x < class.length; x++) {
        if (class[x].selectorText == className) {
            (class[x].cssText) ? alert(class[x].cssText) : alert(classes[x].style.cssText);
        }
    }
}
getStyleRules('.YourClassName');
函数getStyleRules(类名){
var class=document.styleSheets[0]。规则| | document.styleSheets[0]。cssRules;
对于(var x=0;x

完全不知道你在问什么。请你重新表述一下你的问题,再举一个更好的例子。另外,据我所知,javascript支持在所有文件中都可访问的全局变量,试图解决您的问题
document.getElementById("mydiv").style;
function getStyleRules(className) {
    var class = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
    for (var x = 0; x < class.length; x++) {
        if (class[x].selectorText == className) {
            (class[x].cssText) ? alert(class[x].cssText) : alert(classes[x].style.cssText);
        }
    }
}
getStyleRules('.YourClassName');