简体   繁体   English

如何让 JQuery 访问我的 css 样式?

[英]How do I get JQuery to access my css style?

I'm in my _Layout.cshtml page.我在我的_Layout.cshtml页面中。 I'm trying to manipulate my menu bar using css and jquery.我正在尝试使用 css 和 jquery 操作我的菜单栏。

Here's what I'm trying to do.这就是我想要做的。

<script type="text/javascript">
$(document).ready(function () {
    var varElement = document.getElementsByTagName("var");
    for (var i = 0; i < varElement.length; i++) {
        varElement[i].style.fontSize = ".75";
    }
});
</script>

My problem is when I get to varElement[i] .我的问题是当我到达varElement[i] I expected to see style in my intellisense but style is not in the list of properties.我希望在我的智能感知中看到样式,但样式不在属性列表中。 The only items available in the intellisense list are:智能感知列表中唯一可用的项目是:

Constructor
hasOwnProperty
isPrototypeOf
propertyIsEnumerable
toLocalString
toString
valueOf

How do I get JQuery to access my css style?如何让 JQuery 访问我的 css 样式?

In jQuery you can set CSS style as below:jQuery您可以设置CSS style如下:

$('var').css('fontSize', .75)

$('body').css('background', '#f00');

You'll want to use the .css() method.您需要使用 .css() 方法。 I'm not sure how you're using the for loop in your example, but on the jsfiddle is an example that includes a simple for loop, and hopefully that is helpful.我不确定您如何在示例中使用 for 循环,但在 jsfiddle 上有一个示例,其中包含一个简单的 for 循环,希望这会有所帮助。

var changeStuff = function(){
for(i=1;i<=2;i++){
$('#p'+i).css('font-size','3em');

} }; } };

http://jsfiddle.net/6UtyN/1/ http://jsfiddle.net/6UtyN/1/

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

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