[2019.03.16]使用DOM操作函数和CSS选择器来针对已有的HTML进行只凭JS的改动
刚入职的时候看到公司用的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
Elementobject representing the first element in the document that matches the specified set of CSS selectors, ornullis 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)如果你需要找到所有的匹配元素,请使用q
uerySelectorAll。
真的是很方便了。实际案例在下面!
还值得一提的是,这个方法相比于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的改动的更多相关文章
- JS DOM操作 函数 事件 阻止事件冒泡
一 函数 1.字符串函数 s.tolowerCase( ): -- 变小写 s.toupperCase( ): -- 变大写 s.substr( 2 , 8 ): -- 截取 ...
- 2019.03.16 ZJOI2019模拟赛 解题报告
得分: \(100+27+20=147\)(\(T1\)巨水,\(T2,T3\)只能写暴力分) \(T1\):深邃 比较套路的一眼题,显然是一个二分+贪心,感觉就是\(NOIP2018Day1T3\) ...
- jQuery-1.9.1源码分析系列(十一) DOM操作
DOM操作包括append.prepend.before.after.replaceWith.appendTo.prependTo.insertBefore.insertAfter.replaceAl ...
- 【summary】JQuery 相关css、ajax、数据操作函数或方法
总结一下JQuery常用的函数方法,更加系统的整理一下. JQuery遍历的一些函数: 函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集 ...
- 原生JavaScript常用的DOM操作
之前项目一直都是用JQuery或者Vue来做的,确实好用,毕竟帮我们解决了很多浏览器兼容问题,但是后面发现大公司面试题都是要原生Javascript来做,然后我就一脸懵逼哈哈哈,毕竟大公司需要的框架或 ...
- React.js 小书 Lesson21 - ref 和 React.js 中的 DOM 操作
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson21 转载请注明出处,保留原文链接和作者信息. 在 React.js 当中你基本不需要和 DO ...
- ref 和 React.js 中的 DOM 操作
在 React.js 当中你基本不需要和 DOM 直接打交道.React.js 提供了一系列的 on*方法帮助我们进行事件监听,所以 React.js 当中不需要直接调用 addEventListen ...
- javascrtpt DOM操作
DOM DOM:(document object mode)文档对象模型.DOM为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构. 目的就是为了能让js操作html元素而制定的一个规范 DO ...
- 解密jQuery内核 DOM操作的核心函数domManip
domManip是什么 dom即Dom元素,Manip是Manipulate的缩写,连在一起就是Dom操作的意思. .domManip()是jQuery DOM操作的核心函数 对封装的节点操作做了参数 ...
随机推荐
- Java——容器类库框架浅析
前言 通常,我们总是在程序运行过程中才获得一些条件去创建对象,这些动态创建的对象就需要使用一些方式去保存.我们可以使用数组去存储,但是需要注意数组的尺寸一旦定义便不可修改,而我们并不知道程序在运行过程 ...
- 在线生成二维码的API接口
现在很多大网站都有这样的一个功能,使用手机扫描一下网页上的二维码便可快速在手机上访问网站.想要实现这样的功能其实很简单,下面麦布分享几个在线生成网址二维码的API接口.都是采用http协议接口,无需下 ...
- web scraper 抓取分页数据和二级页面内容
如果是刚接触 web scraper 的,可以看第一篇文章. web scraper 是一款免费的,适用于普通用户(不需要专业 IT 技术的)的爬虫工具,可以方便的通过鼠标和简单配置获取你所想要数据. ...
- 玩玩小程序:使用 WebApi 交互打造原生的微信小程序 - 图灵小书架
使用 WebApi 交互打造原生的微信小程序 - 图灵小书架 目录 介绍 源码地址 扫一扫体验 代码分析 其它相关信息(互联网搜集) 介绍 定时抓取图灵社区官网的首页.最热.推荐和最新等栏目的相关图书 ...
- 数据库管理工具DataGrip使用总结(一)
DataGrip是JetBrains公司推出的管理数据库的产品,对于JetBrains公司,开发者肯定都不陌生,IDEA和ReSharper都是这个公司的产品,用户体验非常不错. 下载地址:https ...
- activemq配置安装
1.了解JMS查看百度百科 https://baike.baidu.com/item/JMS/2836691?fr=aladdin 2.了解ActiveMQ https://baike.baidu.c ...
- jquery之冒泡事件介绍以及阻止冒泡
什么是事件冒泡 <div style="width: 200px;height: 200px;background: red;margin: 200px auto;" onc ...
- mac webstorm无法打开 如何使webstorm不卡
场景:在应用程序里删除了原先的webstorm,然后从官网下载了新的安装包,进行安装.安装后,webstorm就再也打不开了. 解决方案:执行以下命令,清楚webstorm所有缓存,然后重新安装 $ ...
- FreeNas搭建踩坑指南(三)
0x00 Dell R730 无法编辑阵列卡Raid FreeNas和Windows Server都做成了uefi启动,装之前删掉了数据盘配置,系统装好后重启发现无法进入阵列卡编辑 0x01 解决方案 ...
- android 记一次解决键盘遮挡问题
文章链接:https://mp.weixin.qq.com/s/1gkMtLu0BTXOUOj6isDjUw 日常android开发过程中,会遇到编辑框输入内容弹出软键盘,往往会出现键盘遮挡内容,或者 ...