duanhuo0577
duanhuo0577
2012-02-15 05:49

如何使用jquery应用内联样式?

已采纳

I want to give some styles using jquery. when I load some popup pages in my website.

I tried like this.

$("#myCountDiv").css({ "bottom":"298","position":"relative"});

$("#myInnerContainer").css({"margin-top:35"});

Both are not working. Please help me. How can I applay these styles to those divs?

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongzi0850 dongzi0850 9年前

    The first example isn't working because "bottom" should be "298px" and the bottom example isn't working because the property needs to be the key and "35" should be "35px".

    Examples:

    $("#myCountDiv").css({bottom: '298px', position: 'relative'});
    $("#myInnerContainer").css({marginTop: '35px'});
    

    Have a look at the more examples here.

    点赞 评论 复制链接分享
  • duanqiao9541 duanqiao9541 9年前

    I always get confused also with that .css() property, there are just too many options and each has their own rule.

    Instead of confusing you with all the options, id' recommend just remembering this one.

    $('div').css({
        'property':'value',
        'bottom':'298x',
        'position':'relative',
        'margin-top':'35px',
    });
    

    It is just like CSS, but you have to put quotes around the property and the value. The confusion comes in when you also can use absolute keys, but when you do that, you have to camelback any key that has a hypen:

    $('div').css({
        bottom:'298x',
        position:'relative',
        marginTop:'35px',
    });
    

    It's better just to stick with the first way.

    点赞 评论 复制链接分享