<span>$('jQuery selector').css({"css property name":"css property value"});</span>
<span>//change paragraph text colour to green </span><span>$('p').css({"color":"green"}); </span> <span>//float all divs with class .left </span><span>$('div.left').css('float'); </span> <span>//change all elements with class .bg-red to have a red background </span><span>$('.bg-red').css({"background-color":"red"});</span>
newimg<span>.css({'background-image': 'url('+newimgsrc+')'}); </span>newimg<span>.css({'position': 'absolute'}); </span>newimg<span>.css({'height': '70px'}); </span>newimg<span>.css({'width': '200px'}); </span>newimg<span>.css({'top': '68px'}); </span>newimg<span>.css({'right': '2px'});</span>
newimg<span>.css({'background-image': 'url('+newimgsrc+')', 'position': 'absolute', 'height': '70px', 'width': '200px', 'top': '68px', 'right': '2px'});</span>
<span>//remove text color from a div </span><span>$('#mydiv').removeClass('colors');</span>
<span>//remove text color from a div </span><span>$('#mydiv').css('color', '');</span>
<span>//change text color from red to green (classes specified in stylesheet) </span><span>$('#div').removeClass('red').addClass('green');</span>
<span>.css( "padding-left", "+=15" )</span>
<span>$('div.example').css('width', function(index) { </span> <span>return index * 50; </span><span>});</span>
<span>$('#myDiv').css('background-image', 'my_image.jpg'); </span><span>// OR </span><span>$('#myDiv').css('background', 'path/to/image.jpg'); </span><span>// OR </span><span>$('#myDiv').css("background-image", "url(/myimage.jpg)"); </span> <span><br /><br /> </span><span><h2>A Full Code Example - Set Div Background Image</h2> </span><span>This is a full example of jQuery Code to set a background image into a div dynamically when the page is loaded. </span> <span>[code lang="js"] </span><span><script type='text/javascript'> </span><span>$(document).ready(function() { </span> <span>//preload image (add timestamp to prevent cache) </span> <span>var newimgsrc = 'https://www.sitepoint.com/wp-content/uploads/jquery4u/2011/03/jquery-plugins2.jpg?' + (new Date().getTime()); </span> <span>var newimg = $('#header'); </span> <span>//replace the image </span> <span>$('#header').css("background-image", "url("+newimgsrc+")"); </span> newimg<span>.css({'background-image': 'url('+newimgsrc+')', 'position': 'absolute', 'height': '70px', 'width': '200px', 'top': '68px', 'right': '2px'}); </span> newimg<span>.show(); </span><span>}); </span><span></script></span>
jQuery允許您使用.css()方法同時更改多個CSS屬性。此方法接受一個對象,您可以在其中定義多個CSS屬性及其新值。以下是一個示例:
$(“ p”)。 css({
“背景色”:“ yellow”,
“ font-size”:“ 200%”
>});
在此示例中,在此示例中,所有段落元素都將其背景顏色變為黃色,並將其font and i jeqle添加到200%
$(“ p”)。 addClass(“亮點”);
在此示例中,“亮點”類將添加到所有段落元素中。
>
> jQuery從元素中刪除CSS類,提供.removeclass()方法以從所選元素中刪除一個或多個類。 Here’s an example:
$("p").removeClass("highlight");
In this example, the “highlight” class will be removed from all paragraph elements.
Yes, jQuery provides the .toggleClass() method which allows you to add a class if it’s not already出現,或刪除它。以下是一個示例:
$(“ p”)。 toggleclass(“亮點”);
在此示例中,“亮點”類將在所有段落元素上進行切換。
>我如何獲得使用jquery的當前屬性值的當前值?您只需要將屬性名稱作為字符串傳遞。以下是一個示例:
var color = $(“ p”)。 css(“ color”);
在此示例中,第一個段落元素的當前顏色將存儲在“ color”變量中。
>我可以使用jQuery來根據其當前值來更改CSS屬性?每個選定的元素都會調用此函數,其返回值將用作屬性的新值。以下是一個示例:
$(“ p”)。 css(“ font-size”,函數(索引,值){
>返回parsefloat(value) * 1.2“ px”;
});
在此示例中,在此示例中,所有代表元素的大小都會增加j的font in Anim je,可以增加20%。屬性?
$("p").animate({
"opacity": 0.5,
"font-size": "200%"
}, 2000);
In this example, the opacity and font size of all paragraph elements will be animated over 2 seconds.
How Can I Use jQuery to Change CSS Properties on Hover?
$(“ p”)。懸停(function(){
$(this).css(“ color”,“ color”,“ red”>},function(){
$(
$(this).css(this).css(cold)當鼠標指針離開時,將其重置為原始顏色。
>我可以使用jQuery在單擊時更改CSS屬性?以下是一個示例:
$(“ p”)。單擊(function(){
>您可以將.scroll()方法與.css()結合使用時將用戶滾動頁面滾動時更改CSS屬性。這是一個示例:
$(window).scroll(function(){
});
>在此示例中,所有段落的顏色所有段落的顏色都會更改為所有段落元素,當用戶scrolls scrolls page the page the page。
以上是jQuery動態地更改CSS - 簡單!的詳細內容。更多資訊請關注PHP中文網其他相關文章!