首页 > 编程知识 正文

原生js格式化css(原生js改变样式)

时间:2023-12-23 11:38:08 阅读:319351 作者:VECS

本文目录一览:

如何使用JS来改变CSS样式

方法:

document.getElementById("xx").style.xxx中的所有属性是什么

盒子标签和属性对照

CSS语法(不区分大小写) JavaScript语法(区分大小写)

border border

border-bottom borderBottom

border-bottom-color borderBottomColor

border-bottom-style borderBottomStyle

border-bottom-width borderBottomWidth

border-color borderColor

border-left borderLeft

border-left-color borderLeftColor

border-left-style borderLeftStyle

border-left-width borderLeftWidth

border-right borderRight

border-right-color borderRightColor

border-right-style borderRightStyle

border-right-width borderRightWidth

border-style borderStyle

border-top borderTop

border-top-color borderTopColor

border-top-style borderTopStyle

border-top-width borderTopWidth

border-width borderWidth

clear clear

float floatStyle

margin margin

margin-bottom marginBottom

margin-left marginLeft

margin-right marginRight

margin-top marginTop

padding padding

padding-bottom paddingBottom

padding-left paddingLeft

padding-right paddingRight

padding-top paddingTop

颜色和背景标签和属性对照

CSS 语法(不区分大小写) JavaScript 语法(区分大小写)

background background

background-attachment backgroundAttachment

background-color backgroundColor

background-image backgroundImage

background-position backgroundPosition

background-repeat backgroundRepeat

color color

样式标签和属性对照

CSS语法(不区分大小写) JavaScript 语法(区分大小写)

display display

list-style-type listStyleType

list-style-image listStyleImage

list-style-position listStylePosition

list-style listStyle

white-space whiteSpace

文字样式标签和属性对照

CSS 语法(不区分大小写) JavaScript 语法(区分大小写)

font font

font-family fontFamily

font-size fontSize

font-style fontStyle

font-variant fontVariant

font-weight fontWeight

文本标签和属性对照

CSS 语法(不区分大小写) JavaScript 语法(区分大小写)

letter-spacing l etterSpacing

line-break lineBreak

line-height lineHeight

text-align textAlign

text-decoration textDecoration

text-indent textIndent

text-justify textJustify

text-transform textTransform

vertical-align verticalAlign

如何还原压缩过的css或Js文件,如下图

1、首先新建一个html文件,命名为test.html。

2、在test.html文件内,使用img标签创建一张图片显示,并设置其id属性为pic。

3、在test.html文件内,使用button标签创建一个按钮,按钮名称为“改变图片样式”。

4、在test.html文件内,给button绑定onclick点击事件,当按钮被点击时,执行cha()函数。

5、在test.html文件内,在js标签内,创建cha()函数,在函数内,使用getElementById()方法获得img元素对象,再使用setAttribute()方法设置img对象的class属性为imagecss。

6、在test.html文件内,使用css定义imagecss的样式,设置为红色5px边框,宽度为300px,就可以了。

js中用cssText设置css样式的简单方法

如果网页中一个

id为“no”的标签,暂且当div标签来tell;

想要在js中设置这个div的css样式,很一般的做法是:

var

obj

=

document.getElementByIdx_x_x('no');

obj.style.width

=

'400px';

obj.style.height

=

'300px';

如果要设置一堆又一堆的css样式呢,太麻烦了把、

一般情况下都会结合css来添加className或者改变className达到想要的效果,但是如果你create一个元素,难道还想这样简单点?那就要想别的办法了~

于是大家就写了一个又一个的函数,经典的两个是:

var

obj

=

document.getElementByIdx_x_x('no');

function

setStyle(obj,

css)

{

for(var

attr

in

obj){

obj.style[attr]

=

css[attr];

}

}

setStyle(obj,{width:"400px",height:"300px"});

当然还有更简单的,cssText:

var

obj

=

document.getElementByIdx_x_x('no');

obj.style.cssText

=

"width:400px;

height:300px;";

当然这种方法对于create的元素初始化css样式来说很简单很方便。

以上就是小编为大家带来的js中用cssText设置css样式的简单方法的全部内容了,希望对大家有所帮助,多多支持~

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。