<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中文网其他相关文章!