1、行内样式获取打印出来
2、内嵌和外链的获取不了
<div style="width:200px;height:200px; background: red;"></div>
var box=document.getElementsByTagName("div")[0];
console.log( box.style.width)
3、style属性是对象(数组对象)
4、可以索引值取值
console.log(box.style[0]);
5、值是字符串,没有设置值得是“” 空
6、cssText="字符串形式的样式" 可以一次性添加多个样式,修改原有的内嵌样式
box.style.cssText="border:5px solid black; width:400px; height:200px;"

7、opacity 透明度(子元素,文本都会有透明的样式)不兼容ie6-7-8

1
box.style.opacity="0.2" (值0-1)

8、alpha(opacity=20)透明度(只有自己透明)兼容ie

box.style.filter="alpha(opacity=20)" //百分数

9、获取body

var body=document.body;

隐藏盒子

var box=document.getElementsByTagName("div")[0];
box.style.cssText="width:200px; height:200px; background:red;";
//隐藏盒子的方法
box.onclick=function () {
this.style.display="none"//常用
this.style.opacity="0"//常用
this.style.visibility="hidden";
}
案例
 
按搜索,然后在按input的时候高亮显示
<div>
<input type="text" >
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<button>搜索</button>
</div>
<script>
丢失鼠标的时候样式消失(工作中经常用到)
var inpArr=document.getElementsByTagName("input");
var button=inpArr[inpArr.length-1].nextElementSibling ||inpArr[inpArr.length-1].nextSibling;
button.onclick=function () {
for(var i=0; i<inpArr.length; i++){
//当button按钮被点击以后,所有的input标签被绑定事件,onfocus事件
inpArr[i].onfocus=function(){
this.style.border="1px solid red";
this.style.backgroundColor="#ccc";
};
//绑定onblur事件,取消样式
inpArr[i].onblur=function(){
this.style.border="";
this.style.backgroundColor="";
}
}
}
</script>

js控制style样式的更多相关文章

  1. js控制div样式显示与隐藏,JS通过点击超链接右边(指定位置)显示一个图标

    原文出自:https://blog.csdn.net/seesun2012 javascript基础篇,老土的方法解决js控制div样式,便于新手理解,粗暴的不能再粗暴,如果你是高手,请忽略! 思路: ...

  2. 013——VUE中多种方式使用VUE控制style样式属性

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. JS 控制CSS样式表

    JS控制CSS所使用的方法: <style> .rule{ display: none; } </style> 你想要改变把他的display属性由none改为inline.  ...

  4. Vue.js 控制css样式

    <script src="https://unpkg.com/vue/dist/vue.js"></script> <style type=" ...

  5. js 控制标记样式

    做一个变色的标签 鼠标移入变为灰色,移除变回原来的颜色,点击变成黑色,再点击变回,如果变成黑色不受移入移除影响. <body> <div class="bt1" ...

  6. vue中通过js控制页面样式方法

    在使用vue.js框架的时候,有时候会希望在页面渲染完成之后,再执行函数方法来处理初始化相关的操作,如果只处理页面位置.宽或者高时,必须要在页面完全渲染之后才可以,页面没有加载完成之前,获取到的宽高不 ...

  7. JS控制菜单样式切换

    $('#subtabs a').each(function (i, ele) { var href = $(ele).attr("href"); if (location.href ...

  8. 用JS控制CSS基本样式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp29 用JS控制CSS基本样式的方法 CSS code .class1 { ...

  9. 内联样式自动出现,一般是js控制写入的

    内联样式自动出现,一般是js控制写入的

随机推荐

  1. Linux which命令详解

    Linux which命令 Linux which命令用于查找文件. which指令会在环境变量$PATH设置的目录里查找符合条件的文件 用法: which [options] [--] COMMAN ...

  2. WampServer的下载方法

    http://www.wampserver.com/ 无法访问 报网络连接错误 2019.01.13 最近要用到Windows+apache+mysql+php,为了追求更快的实现速度和更高的稳定性, ...

  3. 对pytorch中Tensor的剖析

    不是python层面Tensor的剖析,是C层面的剖析. 看pytorch下lib库中的TH好一阵子了,TH也是torch7下面的一个重要的库. 可以在torch的github上看到相关文档.看了半天 ...

  4. Zookeeper 在Windows下的安装过程及测试

    安装jdk 安装Zookeeper. 在官网http://zookeeper.apache.org/下载zookeeper.我下载的是zookeeper-3.4.6版本. 解压zookeeper-3. ...

  5. Letsencrypt SSL免费证书申请(Docker)

    最近需要SSL证书,又不想花钱买,正好看到linux基金会去年底上线了新的开源项目,免费推广SSL遂尝试. Let's Encrypt 介绍 Let’s Encrypt is a free, auto ...

  6. JavaWeb——tomcat manager 403 Access Denied .You are not authorized to view this page.

    403 Access Denied You are not authorized to view this page. If you have already configured the Manag ...

  7. Vue 父组件调用子组件函数的方法

    parent.vue(父组件的内容): <template> <div @click="divClick"> <info-wnd ref=" ...

  8. 【Python】启动迅雷下载

    import subprocess import base64 thunder_path = 'E:\Thunder\Program\Thunder.exe' def Url2Thunder(url) ...

  9. laravel代码生成器的记录

    在使用laravel5.5时,关于代码生成器还有很多的功能不明白 是么回事,在此先记录下来过程. 1.composer reqiure summerblue/generator --dev php a ...

  10. scrapy-pipeline的方法

    scrapy中多个pipeline作用: 一个项目可能需要爬取多个网站,根据每个网站的数据量(处理方式)不同,可创建多个管道 pipeline class SpideranythingPipeline ...