jq基础-css-dom操作

174 阅读1分钟
/* css可以设置也可以返回 */
        /* jq的css方法 无论内部或者外部或者行内样式里面的属性都能获取 */
        // console.log( $('div').css('color'));
        /* 这个方法只能获取行内的样式 */
        // console.log(document.getElementsByTagName('div')[0].style.display);
        /* 原生的获取元素全部的样式 */
        // let a = window.getComputedStyle(document.getElementsByTagName('div')[0],null)
        // console.log(a.display);

        /* height() width() 可以设置和返回 */
        /* height如果是怪异盒模型实际上获取的是content的高度 */
        /* height如果是怪异盒模型实际上设置的是content的高度 */
        // console.log( $('div').height('130px') );
        /* width一样的 */
        /* 最好加单位,这样的话可以明显的区别是px 还是% */
        /* 如果不加默认是px */
        // console.log( $('div').width('100%') );
        /* 返回以像素为单位的top和left坐标。仅对可见元素有效  */
        /* offset会把left和margin-left的值加起来 */
        /* offset会把top和margin-top的值加起来 */
        /* 设置成display:none 是获取不到 left和top的值的 */
        // console.log( $('div').offset() );
        /* offsetParent返回最近的已定位祖先元素(父亲或者爷爷或者祖先只要定位就能获取最近的祖先元素)。
        定位元素指的是元素的CSS position值被设置为relative、absolute或fixed的元素
        */
        // console.log( $('.a').offsetParent().css('background','red') );
        /* 返回第一个匹配元素相对于父元素的位置
 */
//  console.log( $('.a').position() )