最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery .css() function with variables and multiple values - Stack Overflow

programmeradmin2浏览0评论

Weird little snafu. I'm using jQuery's .css() method to change the size of text (long story; no, I can't use media queries) using a variable and I need to add em to it. I'm not sure what the syntax is because there are multiple values for the CSS change.

To illustrate:

This works perfectly. It adds em to the calculated value of victore:

$('h1').css('font-size', victore + 'em');

This doesn't:

$('h1').css({
    'font-size':victore + 'em',
    'line-height':vignelli + 'em';
});

The em needs quotes... but so does the value. Wrapping it in parens didn't work

Weird little snafu. I'm using jQuery's .css() method to change the size of text (long story; no, I can't use media queries) using a variable and I need to add em to it. I'm not sure what the syntax is because there are multiple values for the CSS change.

To illustrate:

This works perfectly. It adds em to the calculated value of victore:

$('h1').css('font-size', victore + 'em');

This doesn't:

$('h1').css({
    'font-size':victore + 'em',
    'line-height':vignelli + 'em';
});

The em needs quotes... but so does the value. Wrapping it in parens didn't work

Share Improve this question edited Dec 17, 2011 at 22:35 ThinkingStiff 65.3k30 gold badges147 silver badges241 bronze badges asked Dec 11, 2011 at 4:51 technopeasanttechnopeasant 7,93933 gold badges93 silver badges151 bronze badges 1
  • 2 On your most recent edit you need to remove the ; after the last em. – ThinkingStiff Commented Dec 11, 2011 at 5:10
Add a comment  | 

3 Answers 3

Reset to default 19

You shouldn't have the quotes around the whole thing:

$('h1').css({
    'font-size': victore + "em",
    'color':'red'
});

Here's a fiddle.

$('h1').css({
    'font-size':victore + "em",
    'color':'red'
});

The font-size value is required to be a string, but that string can be the result of an expression, in your case victore + 'em' should result in the appropriate string.

$('h1').css({ 
    'font-size': victore + 'em', 
    'color': 'red' 
});
发布评论

评论列表(0)

  1. 暂无评论