刚入职的时候看到公司用的HTML日志生成工具附带的Panel,工具不够用,找个Fail还要找半天,于是自己琢磨着添砖加瓦。以前也是个半吊子前端工程师,现在可倒好,想要改页面却连页面生成的模板在哪里都不知道,只有通过改动JavaScript才能实现对页面的修改。

固然,操作DOM有原版的

document.getElementsBy

一族,可是它们get的时候不能通过 class 和 标签 来区分,比如:

<div class="FAIL">
<tr class="FAIL">
<td class="FAIL ">I am row NO.1</td>
<td class="TRACE">I am row NO.2</td>
<td class="DEBUG">I am row NO.3</td>
<td class="ERROR">I am row NO.4</td>
</tr>
</div>

通过Class查找会找出一堆FAIL;通过Tag查找……算了吧;通过ID查找……好吧根本没有定义过ID。

活人总不能被尿憋死,CSS3中增强了对选择器的支持,新特性中有:

document.querySelector

函数家族,他们是:

document.querySelector('tagName.className') 

An Element object representing the first element in the document that matches the specified set of CSS selectors, or null is returned if there are no matches.(By MDN)

返回匹配输入的CSS选择器的第一个元素,如果没有匹配的元素,返回空(null)。

document.querySelectorAll('tagName.className') 

If you need a list of all elements matching the specified selectors, you should use querySelectorAll() instead.(By MDN)

如果你需要找到所有的匹配元素,请使用querySelectorAll

真的是很方便了。实际案例在下面!

还值得一提的是,这个方法相比于Anchor,多了方便的动画和滚动位置设定

document.querySelectorAll(eventName)[index].scrollIntoView({ behavior:"auto, "block: "start", inline: "nearest" });

三个属性分别控制动画、滚动到所选区域的哪里,滚动到本页面的哪里。比起 “herf="#top"”来讲方便了许多啊!

部分案例代码:

 /*
Function to add some new buttons to the panel Input: *None* Returns: *None* Effects: add "to top", "to bottom", "find fail" and "find error" buttons Modified on Mar 2019 by Jack Lyu Advise / Next stage: remove functions and put these buttons inside the HTML pages
*/ function addButtons() {
//adding anchor to page
var pageTop = document.createElement('a');
pageTop.setAttribute("id", "top");
var pageBottom = document.createElement('a');
pageBottom.setAttribute("id", "bottom")
var tableBody = document.getElementById("content");
tableBody.insertBefore(pageTop, tableBody.firstChild);
$(pageBottom).insertAfter(tableBody); //adding link to page
var infoText = document.createElement('p');
infoText.setAttribute("style", "font-size: 1em;margin:0 0 5px 5px");
infoText.innerHTML = "Navigator v1.0<br><div style='color:red;>'>Error(s):" + window.counterError + " Fail(s):" + window.counterFail + "</div>";
var toTop = document.createElement('a');
toTop.setAttribute("href", "#top");
toTop.setAttribute("onclick", "resetCounter('All')");
toTop.setAttribute("style", "color:#333333;margin:5px 0 0 5px;border:2px solid #6CDFEA;");
toTop.innerHTML = "To Top";

// 省略一部分 panleBottom.insertBefore(toBottom, panleBottom.lastChild); //adding "find next fail" button function
var failButton = document.createElement('div');
failButton.setAttribute("style", "margin: 15px 0 0 5px;");
var findNextFail = document.createElement('a');
findNextFail.setAttribute("href", "javascript:void(233)");
findNextFail.setAttribute("onclick", "findNext('tr.FAIL')");
findNextFail.setAttribute("style", "color:#333333;border:3px solid #F02311;margin-left:5px;padding:5px 1.19em 5px 5px;width:30%;text-align:center");
findNextFail.innerHTML = "Next FAIL";
failButton.insertBefore(findNextFail, failButton.lastChild);
//adding "Prev fail" button function
var findFail = document.createElement('a');
findFail.setAttribute("href", "javascript:void(233)");
findFail.setAttribute("onclick", "findNext('tr.FAIL',false)");
findFail.setAttribute("style", "color:#333333;border:3px solid #F02311;padding:5px 1.19em 5px 5px;width:30%;text-align:center");
findFail.innerHTML = "Prev FAIL";
failButton.insertBefore(findFail, failButton.lastChild);

// 省略一部分

//add both button to panle
panleBottom.insertBefore(errorButton, panleBottom.lastChild);
} /*
Function to locate target event Input: eventName Returns: *None*
*/ function findEvent(eventName) {
var list = document.querySelectorAll(eventName);
var tag = eventName.split(".")[1];
for (let index = list.length - 1; index >= 0; index--) {
list[index].setAttribute("id", tag + index);
list[index].firstChild.setAttribute("style", "background-color:#FFC943")
}
} /*
Function to find next event Input: eventName Returns: *None* Notice: Only work on some browsers: Chorme 29 and above(animation work on 61 above), IE8 and above, Firefox 1 and above(animation work on 36 above)
*/ function findNext(eventName, isNext) {
var index;
if (isNext == false) {
addCounter(eventName, 2);
findNext(eventName);
return;
}
else if (eventName == 'tr.ERROR') {
if (pointerError < 1) { resetCounter('tr.ERROR'); }
index = counterError - pointerError;
pointerError--;
}
else if (eventName == 'tr.FAIL') {
if (pointerFail < 1) { resetCounter('tr.FAIL'); }
index = counterFail - pointerFail;
pointerFail--;
} else { alert('Script findNext error, call maintainer for help.'); }
document.querySelectorAll(eventName)[index].scrollIntoView({ block: "start", inline: "nearest" });
}

[2019.03.16]使用DOM操作函数和CSS选择器来针对已有的HTML进行只凭JS的改动的更多相关文章

  1. JS DOM操作 函数 事件 阻止事件冒泡

    一 函数 1.字符串函数 s.tolowerCase( ):    -- 变小写 s.toupperCase( ):   -- 变大写 s.substr( 2 , 8 ):     -- 截取     ...

  2. 2019.03.16 ZJOI2019模拟赛 解题报告

    得分: \(100+27+20=147\)(\(T1\)巨水,\(T2,T3\)只能写暴力分) \(T1\):深邃 比较套路的一眼题,显然是一个二分+贪心,感觉就是\(NOIP2018Day1T3\) ...

  3. jQuery-1.9.1源码分析系列(十一) DOM操作

    DOM操作包括append.prepend.before.after.replaceWith.appendTo.prependTo.insertBefore.insertAfter.replaceAl ...

  4. 【summary】JQuery 相关css、ajax、数据操作函数或方法

    总结一下JQuery常用的函数方法,更加系统的整理一下. JQuery遍历的一些函数: 函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集 ...

  5. 原生JavaScript常用的DOM操作

    之前项目一直都是用JQuery或者Vue来做的,确实好用,毕竟帮我们解决了很多浏览器兼容问题,但是后面发现大公司面试题都是要原生Javascript来做,然后我就一脸懵逼哈哈哈,毕竟大公司需要的框架或 ...

  6. React.js 小书 Lesson21 - ref 和 React.js 中的 DOM 操作

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson21 转载请注明出处,保留原文链接和作者信息. 在 React.js 当中你基本不需要和 DO ...

  7. ref 和 React.js 中的 DOM 操作

    在 React.js 当中你基本不需要和 DOM 直接打交道.React.js 提供了一系列的 on*方法帮助我们进行事件监听,所以 React.js 当中不需要直接调用 addEventListen ...

  8. javascrtpt DOM操作

    DOM DOM:(document object mode)文档对象模型.DOM为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构. 目的就是为了能让js操作html元素而制定的一个规范 DO ...

  9. 解密jQuery内核 DOM操作的核心函数domManip

    domManip是什么 dom即Dom元素,Manip是Manipulate的缩写,连在一起就是Dom操作的意思. .domManip()是jQuery DOM操作的核心函数 对封装的节点操作做了参数 ...

随机推荐

  1. kubernetes系列08—service资源详解

    本文收录在容器技术学习系列文章总目录 1.认识service 1.1 为什么要使用service Kubernetes Pod 是有生命周期的,它们可以被创建,也可以被销毁,然而一旦被销毁生命就永远结 ...

  2. Zuul之Filter详解

    Zuul详解 官方文档:https://github.com/Netflix/zuul/wiki/How-it-Works Zuul的中心是一系列过滤器,能够在HTTP请求和响应的路由过程中执行一系列 ...

  3. [Vue] vue2.0

    vue实例 所有的 Vue 组件都是 Vue 实例,并且接受相同的选项对象 当一个 Vue 实例被创建时,它将 data 对象中的所有的属性加入到 Vue 的响应式系统中.当这些属性的值发生改变时,视 ...

  4. AspNetCore 基于AOP实现Polly的使用

    前言   说起AOP,其实我们在做MVC/API 的时候应该没少接触,比如说各种的Fitter 就是典型的AOP了. 本来在使用Polly的时候我最初的打算是使用过滤器来实现的,后来发现实现起来相当的 ...

  5. EF基于方法的查询语法

    实体框架(Entity Framework )是 ADO.NET 中的一套支持开发面向数据的软件应用程序的技术. LINQ to Entities 提供语言集成查询 (LINQ) 支持,它允许开发人员 ...

  6. 系统设计Design For Failure思想

    系统设计Design For Failure思想 Complex systems fail in spectacular ways. Failure isn't a question of if, b ...

  7. Eclipse 新建jsp文件报错问题

    今天在web工程中新建一个index.jsp文件时,发现会报错,记录一下解决办法. 原因:缺少servlet-api.jar包 所以我们先去下载一个jar包,将它引入我们的工程中,即可. 工程右键-& ...

  8. Python 经典面试题汇总之数据库篇

    数据库和缓存 1.列举常见的关系型数据库和非关系型都有那些? 关系型数据库(需要有表结构) mysql.oracle.splserver.postgresql.db2.sybase 非关系型数据库(是 ...

  9. Docker JDK镜像

    Docker jdk镜像 说明 使用alpine-glibc作为基础镜像 JAVA JDK/JRE以1.8为基准 下载文件 1.下载JDK/JRE压缩包. jre-8u201-linux-x64.ta ...

  10. 『cURL』curl: (6) Could not resolve host无法解析主机地址

    最近在学数据挖掘时,获取数据有两种途径: 开放数据,可以直接使用和存储的数据: 网络数据,通过爬虫或云市场api(付费或免费)获取数据 我通过教程,在阿里云购买一个天气数据api,尝试使用cURL获取 ...