WebAPI 之 DOM

DOM:由W3C定义的一系列接口,可以改变网页内容、结构和样式。


API(Application Programming Interface):应用程序编程,提供函数库。
Web API:是浏览器提供的一套浏览器功能(BOM)和页面元素(DOM)的API。

1,document属性

1.1ID取对象

document.getElementById("time");    //<div id="time">2020-05-04</div>

1.2标签名

document.getElementsByTagName("li");

1.3类名获取对象集合(H5)

document.getElementsByClassName("divclass"); 

1.4指定选择器第一个元素(H5)

document.querySelector('选择器'); 

1)document.querySelector('.id');
       2)document.querySelector('#class');
       3)document.querySelector('li');

1.5指定选择器所有元素(H5)

document.querySelectorAll('选择器'); 

1.6body元素对象

document.body;

1.7html对象

document.documentElement;

1.8创建元素

document.write('<div>123<div>')

注意:直接将内容写入页面的内容流,但文档流执行完毕,则会导致页面全部重绘。

2,元素操作

2.1文件

element.innerText = '123'; //只操作文字,不识别HTML

2.2HTML

element.innerHTML = ‘<span>123<span>’; //文字,HTML都可操作

2.3修改属性(内置属性)

・element.src = 'xxx.jpg';

・element.value = '被点击了'; //<input ...>

・element.disable = true; //button禁用

・element.type = 'text'; //password -> text 应用场景:密码可见

・element.style.backgroundColor = 'pink'; //CSS属性修改,行内样式

・element.ClassName = 'change'; //change为CSS样式名

2.4修改属性(内置或自定义属性)

・element.getAttribute('属性'); //H5规范,自定义属性以【data-】开头

・element.setAttribute('属性','值'); //主要针对自定义属性

・element.removeAttribute('属性'); //移除属性

3,事件

例:

<button id='btn'>唐伯虎</button> //事件源
var btn = document.getElementById('btn'); //获取事件源
btn.onclick = function(){ alert('点秋香');} //点击事件,方法体

3.1常用事件

3.1.1点击事件

element.onclick = function(){} 

3.1.2获取焦点

element.onfocus = function(){}

3.1.3失去焦点

element.onblur = function(){}

3.2事件监听

若对同一对象注册多个相同事件,则事件会出现覆盖现象,推荐使用监听注册方式,从而可以避免事件覆盖现象发生。

例子:

btn.addEventListener('click',function(){ })

3.3删除事件(解绑)

1)div.onclick = null ; //传统解绑方式

2)eventTarget.removeEventListener(type,Listener[,useCapture]);

3)eventTarget.detachEvent(eventNameWithOn,callback);

3.4DOM事件流

1)捕获阶段 //网景公司提出

2)当前目标阶段

3)冒泡阶段 //IE提出

注意:
      JS只可执行1)3)中的一个;
      onclick,attachEvent只能得到冒泡阶段;
      onblur,onfocus,onmouseenter,onmouseleave没有冒泡阶段。

语法:

addEventListener(type,function(){},flag); //flag:true:捕获阶段,false:冒泡阶段

3.5事件对象

IE6,7,8:window.event
IE9+         :  event或e
e = e || window.evet; //兼容性处理

1)e.targer //触发事件对象(谁出发的) IE6,7,8:e.srcElement

2)e.type //事件类型,点击还是鼠标经过等等,注意不带on

3)e.preventDefault(); //阻止默认行为,IE6,7,8:e.returnValue

4)e.stopPropagation(); //阻止冒泡,IE6,7,8:cancelBubble

3.6事件委托

用处:不必给每个子节点添加时间监听,而是在父节点上添加,提高性能。常与e.target搭配使用。

3.7补充技巧

3.7.1阻止右键菜单

document.addEventListener('contextmenu',function(e){ e.preventDefault();})

3.7.2阻止选中

document.addEventListener('selectstart',function(e){ e.preventDefault(); })

4,节点Node

4.1节点类型(nodeType)

1)nodeType=1;//元素节点

2)nodeType=2; //属性节点

3)nodeType=3; //文本节点(文字,空格,换行等)

4.2查找

3.2.1父节点

element.parentNode;

3.2.2子节点

parentNode.childNodes; //集合(含元素和文本)

parentNode.childNodes[i].nodeType == 1; //元素节点

parentNode.children; //只取子节点的元素节点

parentNode.firstElementChild; //第一个元素子节点(IE9+)

parentNode.lastElementChild; //最后一个元素子节点(IE9+)

parentNode.children[0]; //第一个元素子节点

parentNode.children[ol.children,lenth - 1]; // 最后一个元素子节点

3.2.3兄弟节点

node.nextSibling; //下一个兄弟节点,无则返回null,包含所有节点

node.previousSibling; //上一个兄弟节点

node.nextElementSiling; //下一个兄弟元素节点(IE9+)

node.previousElementSibling; //上一个兄弟元素节点(IE9+)

4.3创建节点

1)document.createElement('tagName'); //动态创建

2)node.appendChild(child); //追加节点(最后)

3)node.insertBefore(child,指定元素); //插到指定子元素前面

4.3删除节点

node.removeChild(child);

4.4复制节点

node.cloneNode(); //只复制节点本身,不复制子节点,浅拷贝

node.cloneNode(true); //深拷贝,复制本身及内容

5,重要事件

5.1鼠标事件

5.1.1鼠标在可视区的XY坐标

e.clientX,e.clientY

5.1.2鼠标在浏览器窗口的XY坐标

e.pageX,e.pageY

5.1.3鼠标在电脑屏幕的XY坐标

e.screenX,e.screenY

5.1.4鼠标移动事件

mousemove

案例(天使移动)://pic为绝对定位

document.addEventListener('mousemove',function(e){

pic.style.left =  e.pageX + 'px';

pic.style.top=  e.pageY + 'px';

})

5.2键盘事件

5.2.1键盘弹起时触发:onKeyUp

5.2.2键盘按下时触发:onKeyDown

5.2.3键盘按下时触发,不识别功能键:onKeyPress(功能键:ctrl,shift,alt,方向键)

5.2.4按键的ASCII值:keyCode(区分大小写)

 

java远程访问linux服务器操作 远程执行shll脚本或者命令、上传下载文件 package com.szkingdom.kfit.bank.ccbDirectShortcut.helper; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.SCPClient; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; import common.Logger; import org.apache.commons.lang.StringUtils; import java.io.*; import java.util.logging.Level; /** * SCP远程访问Linux服务器读取文件 * User: boyer * Date: 17-12-7 * Time: 下午3:22 * To change this template use File | Settings | File Templates. */ public class ScpClient { //字符编码默认是utf-8 private static String DEFAULTCHART="UTF-8"; protected static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(ScpClient.class); static private ScpClient instance; private Connection conn; static synchronized public ScpClient getInstance(String IP, int port, String username, String passward) { if (instance == null) { instance = new ScpClient(IP, port, username, passward); } return instance; } public ScpClient(String IP, int port, String username, String passward) { this.ip = IP; this.port = port; this.username = username; this.password = passward; } private String ip; private int port; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } /** * 远程登录linux的主机 * @author Ickes * @since V0.1 * @return * 登录成功返回true,否则返回false */ public Boolean login(){ boolean flg=false; try { conn = new Connection(ip); conn.connect();//连接 flg=conn.authenticateWithPassword(username, password);//认证 } catch (IOException e) { e.printStackTrace(); } return flg; } /** * 下载文件 * @param remoteFile 远程文件地址 * @param localTargetDirectory 本地目录地址 */ public void getFile(String remoteFile, String localTargetDirectory) { try { if(login()){ SCPClient client = new SCPClient(conn); client.get(remoteFile, localTargetDirectory); conn.close(); } } catch (IOException ex) { log.error(ex); } } /** * 上传文件 * @param localFile 本地目录地址 * @param remoteTargetDirectory 远程目录地址 */ public void putFile(String localFile, String remoteTargetDirectory) { try { if(login()){ SCPClient client = new SCPClient(conn); client.put(localFile, remoteTargetDirectory); conn.close(); } } catch (IOException ex) { log.error(ex); } } /** * 上传文件 * @param localFile 本地目录地址 * @param remoteFileName 重命名 * @param remoteTargetDirectory 远程目录地址 * @param mode 默认0600权限 rw 读写 */ public void putFile(String localFile, String remoteFileName,String remoteTargetDirectory,String mode) { try { if(login()){ SCPClient client = new SCPClient(conn); if((mode == null) || (mode.length() == 0)){ mode = "0600"; } client.put(localFile, remoteFileName, remoteTargetDirectory, mode); //重命名 ch.ethz.ssh2.Session sess = conn.openSession(); String tmpPathName = remoteTargetDirectory +File.separator+ remoteFileName; String newPathName = tmpPathName.substring(0, tmpPathName.lastIndexOf(".")); sess.execCommand("mv " + remoteFileName + " " + newPathName);//重命名回来 conn.close(); } } catch (IOException ex) { log.error(ex); } } public static byte[] getBytes(String filePath) { byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(1024*1024); byte[] b = new byte[1024*1024]; int i; while ((i = fis.read(b)) != -1) { byteArray.write(b, 0, i); } fis.close(); byteArray.close(); buffer = byteArray.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } /** * @author Ickes * 远程执行shll脚本或者命令 * @param cmd * 即将执行的命令 * @return * 命令执行完后返回的结果值 * @since V0.1 */ public String execute(String cmd){ String result=""; try { if(login()){ Session session= conn.openSession();//打开一个会话 session.execCommand(cmd);//执行命令 result=processStdout(session.getStdout(),DEFAULTCHART); //如果为得到标准输出为空,说明脚本执行出错了 if(StringUtils.isBlank(result)){ result=processStdout(session.getStderr(),DEFAULTCHART); } conn.close(); session.close(); } } catch (IOException e) { e.printStackTrace(); } return result; } /** * @author Ickes * 远程执行shll脚本或者命令 * @param cmd * 即将执行的命令 * @return * 命令执行成功后返回的结果值,如果命令执行失败,返回空字符串,不是null * @since V0.1 */ public String executeSuccess(String cmd){ String result=""; try { if(login()){ Session session= conn.openSession();//打开一个会话 session.execCommand(cmd);//执行命令 result=processStdout(session.getStdout(),DEFAULTCHART); conn.close(); session.close(); } } catch (IOException e) { e.printStackTrace(); } return result; } /** * 解析脚本执行返回的结果集 * @author Ickes * @param in 输入流对象 * @param charset 编码 * @since V0.1 * @return * 以纯文本的格式返回 */ private String processStdout(InputStream in, String charset){ InputStream stdout = new StreamGobbler(in); StringBuffer buffer = new StringBuffer();; try { BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset)); String line=null; while((line=br.readLine()) != null){ buffer.append(line+"\n"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer.toString(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值