最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Is it possible to pass variables to jquery's css function? - Stack Overflow

matteradmin3PV0评论

I'd like to set css of a div element dynamically using jQuery css() function instead of using string literals/ string constants for the css() function. Is it possible?

Instead of using the following codes with string literals:

$('#myDiv').css('color', '#00ff00');

I would like to use variables to set css for #myDiv element like

Version 1:

var propertyName = get_propery_name(myVariable1); // function get_propery_name() returns a string like 'background-color'
var value = get_value(myVariable2) ; //  function get_value() returns a string like '#00ff00'
$('#myDiv').css(propertyName, value);

Version 2: (just hard coded to see if they work without calling custom functions like version 1 above):

var propertyName = 'background-color';
var value = '#00ff00';
$('#divLeftReportView').css(propertyName, value);

Both variable versions of codes do not work. Please help. Thanks.

I'd like to set css of a div element dynamically using jQuery css() function instead of using string literals/ string constants for the css() function. Is it possible?

Instead of using the following codes with string literals:

$('#myDiv').css('color', '#00ff00');

I would like to use variables to set css for #myDiv element like

Version 1:

var propertyName = get_propery_name(myVariable1); // function get_propery_name() returns a string like 'background-color'
var value = get_value(myVariable2) ; //  function get_value() returns a string like '#00ff00'
$('#myDiv').css(propertyName, value);

Version 2: (just hard coded to see if they work without calling custom functions like version 1 above):

var propertyName = 'background-color';
var value = '#00ff00';
$('#divLeftReportView').css(propertyName, value);

Both variable versions of codes do not work. Please help. Thanks.

Share Improve this question edited Aug 8, 2012 at 20:12 Brian Webster 30.9k51 gold badges157 silver badges227 bronze badges asked May 16, 2012 at 14:02 user1219702user1219702 2211 gold badge4 silver badges12 bronze badges 2
  • 1 There is nothing special in it. Both variants should work well: jsfiddle/UQWyH – VisioN Commented May 16, 2012 at 14:03
  • Did this answer your question? Please accept an answer if one of them helped you – Brian Webster Commented Aug 8, 2012 at 20:12
Add a ment  | 

5 Answers 5

Reset to default 2

Both of your examples will work just fine. I would suggest just a bit cleaner approach (personal syntax preference):

$(document).ready(function () {
    $('#myDiv').css(get_propery_name(myVariable1), get_value(myVariable2));
}

Here's a working fiddle.

If you want to take it a step further, you can return a CSS map instead of strings:

$('#divLeftReportView').css(GetCssMap("foo"));

function GetCssMap(mapIdentifier) {
    return { "background-color" : "#00ff00" }
}

Here's a working fiddle.

The code you posted here should work. I have done both versions of what you are trying to do several times. If it is not working, there is a good chance that something is wrong somewhere else in your javascript OR that you do not have the correct selector/id for the element(s) which you want to change.

Try adding alert("test"); immediately after $('#divLeftReportView').css(propertyName, value);. If you get the popup saying "test" then the problem is with your selector. If not, then the problem is a bug in your javascript.

Also try adding $('#divLeftReportView').css("background-color", "#00ff00"); in the same place. That will confirm whether or not the selector is working.

Seems to work fine at http://jsfiddle/gaby/6wHtW/

Make sure you run your code after the DOM ready event..

$(function(){
    var propertyName = 'background-color';
    var value = '#00ff00';
    $('#divLeftReportView').css(propertyName, value);
});

otherwise your elements might not be present in the DOM..

You can also pass multiple CSS parameters within one variable as an array:

$(function(){
    var divStyle = {'background-color': '#00ff00', 'color': '#000000'}
    $('#divID').css(divStyle);
});

yes, using jQuery attr method you can change css dynamically

var height=$(".sampleClass1").innerHeight();
$('.sammpleClass2').attr('style', 'min-height:'+height+' !important');
Post a comment

comment list (0)

  1. No comments so far