刚入职的时候看到公司用的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. Java服务器内存过高&CPU过高问题排查

    一.内存过高 1.内存过高一般有两种情况:内存溢出和内存泄漏 (1)内存溢出:程序分配的内存超出物理机的内存大小,导致无法继续分配内存,出现OOM报错 (2)内存泄漏:不再使用的对象一直占据着内存不释 ...

  2. Tkinter小技巧:如何为窗口右上角的‘x’添加一个自定义的响应函数

    不废话,直接上代码 import tkinter as tk from tkinter import messagebox main_window = tk.Tk() main_window.geom ...

  3. 微信H5页面 会被软键盘顶起来

    问题描述:H5页面在微信中打开,input输入框获取焦点时,页面被软键盘顶上去:关闭软键盘时,页面不会自动下来(恢复初始状态) H5页面在微信中初始状态如下图: input输入框获取焦点时,页面被软键 ...

  4. 微信小程序 canvas 绘制圆形状

    page({ // 绘制canvas drawCanvas:function(){ const ctx = wx.createCanvasContext('poster') // 画圆形二维码 thi ...

  5. Dynamics 365中极特殊语言的文字搜索结果异常

    微软动态CRM专家罗勇 ,回复316或者20190314可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 有些比较少见的问题, ...

  6. ext当表单中的输入项为必填时,输入项label后显示红色的*

    form表单里,当输入项为必填项时,需要将对应item的allowblank属性设置为true,如果item的label后面自带红色的*,表单中哪些输入项是“必填”,哪些输入项是“非必填”,一眼望去清 ...

  7. java获取机器IP地址常用方法

    private String getHostIP(){ Enumeration<NetworkInterface> allNetInterfaces = null; String resu ...

  8. hbase rowkey 的设计

    什么是rowkey Hbase是一个分布式的.面向列的数据库,它和一般关系型数据库的最大区别是:HBase很适合于存储非结构化的数据,还有就是它基于列的而不是基于行的模式. Hbase是采用K,V存储 ...

  9. SQL SERVER 2012 AlwaysOn– 数据库层面 02

    搭建 AlwaysOn 是件非常繁琐的工作,需要从两方面考虑,操作系统层面和数据库层面,AlwaysOn 非常依赖于操作系统,域控,群集,节点等概念: DBA 不但要熟悉数据库也要熟悉操作系统的一些概 ...

  10. 从0开始的Python学习018更多的Python内容

    特殊的方法 之前学习的都是一些常用的方法,为了使我们的学习更加的完整,我们在这里学习一些特殊的方法. 一般说来,特殊的方法都被用来模仿某个行为.例如,如果你想要为你的类使用x[key]这样的索引操作( ...