Jquery css on array

Set css defined in an array in jQuery

The following code shows how to set css defined in an array.

Example

 html> head> script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">  script type="text/javascript"> $(document).ready(function()!-- w w w.j a v a 2 s . c o m--> $("b").hover(function () < $(this).css('background-color' : 'yellow', 'font-weight' : 'bolder'>); >, function () < var cssObj = < 'background-color' : '#ddd', 'font-weight' : '', 'color' : 'red' > $(this).css(cssObj); >); >);  body> body> b>java2s.co m   

The code above generates the following result.

Источник

jQuery css using arrays as values

The following tutorial shows you how to do «jQuery css using arrays as values».

The result is illustrated in the iframe.

You can check the full source code and open it in another tab using the links.

Javascript Source Code

The Javascript source code to do «jQuery css using arrays as values» is

var myArray = [ 'float':'left','-webkit-border-top-left-radius':'20px','-webkit-border-bottom-left-radius':'20px','-webkit-border-top-right-radius':'0px','-webkit-border-bottom-right-radius':'20px'> //more objects. ]; $(function()< $("#myDiv").click(function()< $(this).css(myArray[0]); >); >);
html> head> title>SO jQuery Fiddle meta name="viewport" content="width=device-width, initial-scale=1"> script type="text/javascript" src="https://code.jquery.com/jquery-1.4.3.min.js" > script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js">  link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/ui-lightness/jquery-ui.css"> style id="compiled-css" type="text/css"> div < border: solid 1px #ddd; > !-- w ww . d e m o 2 s . c o m-->   body> div id="myDiv">Click me! script type='text/javascript'> var myArray = [ 'float':'left','-webkit-border-top-left-radius':'20px','-webkit-border-bottom-left-radius':'20px','-webkit-border-top-right-radius':'0px','-webkit-border-bottom-right-radius':'20px'> //more objects. ]; $(function()< $("#myDiv").click(function()< $(this).css(myArray[0]); >); >);   

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Jquery css on array

Get the value of a computed style property for the first element in the set of matched elements or set one or more CSS properties for every matched element.

Contents:

.css( propertyName ) Returns: String

Description: Get the computed style properties for the first element in the set of matched elements.

version added: 1.0 .css( propertyName )

version added: 1.9 .css( propertyNames )

The .css() method is a convenient way to get a computed style property from the first matched element, especially in light of the different ways browsers access most of those properties (the getComputedStyle() method in standards-based browsers versus the currentStyle and runtimeStyle properties in Internet Explorer prior to version 9) and the different terms browsers use for certain properties. For example, Internet Explorer’s DOM implementation refers to the float property as styleFloat , while W3C standards-compliant browsers refer to it as cssFloat . For consistency, you can simply use «float» , and jQuery will translate it to the correct value for each browser.

Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both .css( «background-color» ) and .css( «backgroundColor» ) . This means mixed case has a special meaning, .css( «WiDtH» ) won’t do the same as .css( «width» ) , for example.

Note that the computed style of an element may not be the same as the value specified for that element in a style sheet. For example, computed styles of dimensions are almost always pixels, but they can be specified as em, ex, px or % in a style sheet. Different browsers may return CSS color values that are logically but not textually equal, e.g., #FFF, #ffffff, and rgb(255,255,255).

Retrieval of shorthand CSS properties (e.g., margin , background , border ), although functional with some browsers, is not guaranteed. For example, if you want to retrieve the rendered border-width , use: $( elem ).css( «borderTopWidth» ) , $( elem ).css( «borderBottomWidth» ) , and so on.

An element should be connected to the DOM when calling .css() on it. If it isn’t, jQuery may throw an error.

As of jQuery 1.9, passing an array of style properties to .css() will result in an object of property-value pairs. For example, to retrieve all four rendered border-width values, you could use $( elem ).css([ «borderTopWidth», «borderRightWidth», «borderBottomWidth», «borderLeftWidth» ]) .

As of jQuery 3.2, CSS Custom Properties (also called CSS Variables) are supported: $( «p» ).css( «—custom-property» ) . Note that you need to provide the property name as-is, camelCasing it won’t work as it does for regular CSS properties.

Examples:

Get the background color of a clicked div.

html>
html lang="en">
head>
meta charset="utf-8">
title>css demo title>
style>
div
width: 60px;
height: 60px;
margin: 5px;
float: left;
>
style>
script src="https://code.jquery.com/jquery-3.7.0.js"> script>
head>
body>
span id="result">  span>
div style="background-color:blue;"> div>
div style="background-color:rgb(15,99,30);"> div>
div style="background-color:#123456;"> div>
div style="background-color:#f11;"> div>
script>
$( "div" ).on( "click", function( )
var color = $( this ).css( "background-color" );
$( "#result" ).html( "That div is " + color + "." );
>);
script>
body>
html>

Demo:

Get the width, height, text color, and background color of a clicked div.

html>
html lang="en">
head>
meta charset="utf-8">
title>css demo title>
style>
div
height: 50px;
margin: 5px;
padding: 5px;
float: left;
>
#box1
width: 50px;
color: yellow;
background-color: blue;
>
#box2
width: 80px;
color: rgb(255, 255, 255);
background-color: rgb(15, 99, 30);
>
#box3
width: 40px;
color: #fcc;
background-color: #123456;
>
#box4
width: 70px;
background-color: #f11;
>
style>
script src="https://code.jquery.com/jquery-3.7.0.js"> script>
head>
body>
p id="result">  p>
div id="box1">1 div>
div id="box2">2 div>
div id="box3">3 div>
div id="box4">4 div>
script>
$( "div" ).on( "click", function( )
var html = [ "The clicked div has the following styles:" ];
var styleProps = $( this ).css([
"width", "height", "color", "background-color"
]);
$.each( styleProps, function( prop, value )
html.push( prop + ": " + value );
>);
$( "#result" ).html( html.join( "
"
) );
>);
script>
body>
html>

Demo:

.css( propertyName, value ) Returns: jQuery

Description: Set one or more CSS properties for the set of matched elements.

version added: 1.0 .css( propertyName, value )

version added: 1.4 .css( propertyName, function )

A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old value as arguments.

version added: 1.0 .css( properties )

As with the .prop() method, the .css() method makes setting properties of elements quick and easy. This method can take either a property name and value as separate parameters, or a single object of key-value pairs.

Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both .css(< "background-color": "#ffe", "border-left": "5px solid #ccc" >) and .css() . Notice that with the DOM notation, quotation marks around the property names are optional, but with CSS notation they're required due to the hyphen in the name.

When a number is passed as the value, jQuery will convert it to a string and add px to the end of that string. If the property requires units other than px , convert the value to a string and add the appropriate units before calling the method.

When using .css() as a setter, jQuery modifies the element's style property. For example, $( "#mydiv" ).css( "color", "green" ) is equivalent to document.getElementById( "mydiv" ).style.color = "green" . Setting the value of a style property to an empty string — e.g. $( "#mydiv" ).css( "color", "" ) — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. As a consequence, the element's style for that property will be restored to whatever value was applied. So, this method can be used to cancel any style modification you have previously performed. It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or element. Warning: one notable exception is that, for IE 8 and below, removing a shorthand property such as border or background will remove that style entirely from the element, regardless of what is set in a stylesheet or element.

Note: .css() doesn't support !important declarations. So, the statement $( "p" ).css( "color", "red !important" ) does not turn the color of all paragraphs in the page to red as of jQuery 3.6.0. Do not depend on that not working, though, as a future version of jQuery may add support for such declarations. It's strongly advised to use classes instead; otherwise use a jQuery plugin.

As of jQuery 1.8, the .css() setter will automatically take care of prefixing the property name. For example, take .css( "user-select", "none" ) in Chrome/Safari will set it as -webkit-user-select , Firefox will use -moz-user-select , and IE10 will use -ms-user-select .

As of jQuery 1.6, .css() accepts relative values similar to .animate() . Relative values are a string starting with += or -= to increment or decrement the current value. For example, if an element's padding-left was 10px, .css( "padding-left", "+=15" ) would result in a total padding-left of 25px.

As of jQuery 1.4, .css() allows us to pass a function as the property value:

$( "div.example" ).css( "width", function( index )
return index * 50;
>);

This example sets the widths of the matched elements to incrementally larger values.

Note: If nothing is returned in the setter function (ie. function( index, style )<> ) , or if undefined is returned, the current value is not changed. This is useful for selectively setting values only when certain criteria are met.

As of jQuery 3.2, CSS Custom Properties (also called CSS Variables) are supported: $( "p" ).css( "--custom-property", "value" ) . Note that you need to provide the property name as-is, camelCasing it won't work as it does for regular CSS properties.

Examples:

Change the color of any paragraph to red on mouseover event.

Источник

jQuery Create Custom grid css depending on Array Length

The following tutorial shows you how to do "jQuery Create Custom grid css depending on Array Length".

The result is illustrated in the iframe.

You can check the full source code and open it in another tab using the links.

Javascript Source Code

The Javascript source code to do "jQuery Create Custom grid css depending on Array Length" is

var data = [1,2,3,4,5,6,7]; function makeRow(a,b)< a = !!a ? a : ""; b = !!b ? b : ""; return "
"+a+"
" + "
"+b+"
"
; > for(var i=0; i< data.length; i = i + 2)< document.getElementById("parent").innerHTML += (makeRow(data[i], data[i+1])); >
html> head> meta name="viewport" content="width=device-width, initial-scale=1"> script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.js" > style id="compiled-css" type="text/css"> .cell!-- w w w . d e m o2 s. c o m --> display:table-cell; height:20px; width:20px; border:1px solid black; > .row< display:block; >  body> p id="parent">  script type='text/javascript'> var data = [1,2,3,4,5,6,7]; function makeRow(a,b)< a = !!a ? a : ""; b = !!b ? b : ""; return "
"+a+"
" + "
"+b+"
"
; > for(var i=0; i< data.length; i = i + 2)< document.getElementById("parent").innerHTML += (makeRow(data[i], data[i+1])); >

  • jQuery div heights using CSS grid
  • jQuery div heights using CSS grid (Demo 2)
  • jQuery div heights using CSS grid (Demo 3)
  • jQuery Create Custom grid css depending on Array Length
  • jQuery CSS DIV Layout over Grid
  • jQuery CSS fix for webgrid's pager method using Razor and MVC3
  • jQuery CSS Grid fixed height and variable width

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Читайте также:  Создание HTML-таблиц
Оцените статью