[Intern][2019.03.16]针对已有的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 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"”来讲方便了许多啊!

部分案例代码:

  1 /*
  2 Function to add some new buttons to the panel
  3
  4 Input: *None*
  5
  6 Returns: *None*
  7
  8 Effects: add "to top", "to bottom", "find fail" and "find error" buttons
  9
 10 Modified on Mar 2019 by Jack Lyu
 11
 12 Advise / Next stage: remove functions and put these buttons inside the HTML pages
 13 */
 14
 15 function addButtons() {
 16     //adding anchor to page
 17     var pageTop = document.createElement(‘a‘);
 18     pageTop.setAttribute("id", "top");
 19     var pageBottom = document.createElement(‘a‘);
 20     pageBottom.setAttribute("id", "bottom")
 21     var tableBody = document.getElementById("content");
 22     tableBody.insertBefore(pageTop, tableBody.firstChild);
 23     $(pageBottom).insertAfter(tableBody);
 24
 25     //adding link to page
 26     var infoText = document.createElement(‘p‘);
 27     infoText.setAttribute("style", "font-size: 1em;margin:0 0 5px 5px");
 28     infoText.innerHTML = "Navigator v1.0<br><div style=‘color:red;>‘>Error(s):" + window.counterError + " Fail(s):" + window.counterFail + "</div>";
 29     var toTop = document.createElement(‘a‘);
 30     toTop.setAttribute("href", "#top");
 31     toTop.setAttribute("onclick", "resetCounter(‘All‘)");
 32     toTop.setAttribute("style", "color:#333333;margin:5px 0 0 5px;border:2px solid #6CDFEA;");
 33     toTop.innerHTML = "To Top";
 // 省略一部分

 42     panleBottom.insertBefore(toBottom, panleBottom.lastChild);
 43
 44     //adding "find next fail" button function
 45     var failButton = document.createElement(‘div‘);
 46     failButton.setAttribute("style", "margin: 15px 0 0 5px;");
 47     var findNextFail = document.createElement(‘a‘);
 48     findNextFail.setAttribute("href", "javascript:void(233)");
 49     findNextFail.setAttribute("onclick", "findNext(‘tr.FAIL‘)");
 50     findNextFail.setAttribute("style", "color:#333333;border:3px solid #F02311;margin-left:5px;padding:5px 1.19em 5px 5px;width:30%;text-align:center");
 51     findNextFail.innerHTML = "Next FAIL";
 52     failButton.insertBefore(findNextFail, failButton.lastChild);
 53     //adding "Prev fail" button function
 54     var findFail = document.createElement(‘a‘);
 55     findFail.setAttribute("href", "javascript:void(233)");
 56     findFail.setAttribute("onclick", "findNext(‘tr.FAIL‘,false)");
 57     findFail.setAttribute("style", "color:#333333;border:3px solid #F02311;padding:5px 1.19em 5px 5px;width:30%;text-align:center");
 58     findFail.innerHTML = "Prev FAIL";
 59     failButton.insertBefore(findFail, failButton.lastChild);
 // 省略一部分
 80     //add both button to panle
 81     panleBottom.insertBefore(errorButton, panleBottom.lastChild);
 82 }
 83
 84 /*
 85 Function to locate target event
 86
 87 Input: eventName
 88
 89 Returns:  *None*
 90 */
 91
 92 function findEvent(eventName) {
 93     var list = document.querySelectorAll(eventName);
 94     var tag = eventName.split(".")[1];
 95     for (let index = list.length - 1; index >= 0; index--) {
 96         list[index].setAttribute("id", tag + index);
 97         list[index].firstChild.setAttribute("style", "background-color:#FFC943")
 98     }
 99 }
100
101 /*
102 Function to find next event
103
104 Input: eventName
105
106 Returns:  *None*
107
108 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)
109 */
110
111 function findNext(eventName, isNext) {
112     var index;
113     if (isNext == false) {
114         addCounter(eventName, 2);
115         findNext(eventName);
116         return;
117     }
118     else if (eventName == ‘tr.ERROR‘) {
119         if (pointerError < 1) { resetCounter(‘tr.ERROR‘); }
120         index = counterError - pointerError;
121         pointerError--;
122     }
123     else if (eventName == ‘tr.FAIL‘) {
124         if (pointerFail < 1) { resetCounter(‘tr.FAIL‘); }
125         index = counterFail - pointerFail;
126         pointerFail--;
127     }
128
129     else { alert(‘Script findNext error, call maintainer for help.‘); }
130     document.querySelectorAll(eventName)[index].scrollIntoView({ block: "start", inline: "nearest" });
131 }
132

原文地址:https://www.cnblogs.com/jackablack/p/10567368.html

时间: 2024-08-13 10:52:37

[Intern][2019.03.16]针对已有的HTML如何只凭JS改动?的相关文章

大数据学习2019.03.16

Java EE进阶——面向对象程序设计01 资料库的故事 package dome; public class CD { private String title; private String artist; private int numOfTracks; private int playingTime; private boolean gotIt = false; private String comment; public CD(String title, String artist,

【谜客帝国】第148届梦中人主擂谜会(2019.03.15)

[谜客帝国]第148届梦中人主擂谜会(2019.03.15) 主持:瓷    计分:小白 1.夫人囚禁于何处(5字对景点位置咨询语)娘子关在哪 2.找你去战胜排名第二的人(外教练)索尔斯克亚 3.二月里来换新装(莫言小说<蛙>人物)王胆 4.一心除皇上,自然搭上命(科技名词)全息 5.布什更二(货币冠量)一便士 6.“身形婀娜,虽裹在一袭宽大缁衣之中,仍掩不住窈窕娉婷之态”(3字古埃及国王,卷帘格)美尼斯 [注:面为<笑傲江湖>中对仪琳的描写,美尼斯为是埃及第一王朝的开国国王] 7

【谜客帝国】第149届汝隅主擂谜会(2019.03.30)

[谜客帝国]第149届汝隅主擂谜会(2019.03.30) 主持:瓷    计分:雪宝 1.赞歌唱6.1(动漫人物•卷帘)哆啦美 [赞扣美,1和6唱出来是哆啦] 2.“枕前泪共阶前雨,隔着窗儿滴到明”(明人)戚继光 3.湘玉呼展堂,赶紧去上工(4字对酒介绍语,含酒名)叫老白干 4.留下雄信待聚会(金融词二)存单.通汇 5.五音未闻宫徵羽(国际事件•重门)贸易战 [注:先扣商角,重门出底] 6.殷功是否尚存生(对歌手到场情况询问语2+3)崔健在不在 [注:崔护,字殷功] 7.“孤与云长,誓同生死:

Wed Nov 01 13:03:16 CST 2017 WARN: Establishing SSL connection without server&#39;s identity verification is not recommended.

报错:Wed Nov 01 13:03:16 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option

2019.03.30 Dialog demo 一个标准使用的dialog程序

1 PROGRAM zdemo_dialog. 2 3 INCLUDE zdemo_dialogtop. 4 INCLUDE zdemo_dialogo01. 5 INCLUDE zdemo_dialogi01. 6 INCLUDE zdemo_dialogf01. 7 INCLUDE zdemo_dialoghelp. *&---------------------------------------------------------------------* *& 包含       

2019.5.16 课后练习。简易购物车程序

根据之前看过的知识点,以自己的思路先写出了一个程序,后续继续老师的操作再进行优化. 1 salary = int(input("请输入您的工资(单位 元):")) 2 3 print("以下是可购清单:") 4 5 shopping_list = ['','iphone','computer','book','apple_juice','bread','cake']#商品列表 6 commodity_prices = [0,6000,7000,2,3,3,5] #商

2017/03/16学习笔记

//void oper(int a,int b) const ;==>void oper(const className* const this,int a,int b); 运算符重载 所谓重载,就是重新赋予新的含义.函数重载就是对一个已有的函数赋予新的含义,使之实现新功能,因此,一个函数名就可以用来代表不同的功能函数,也就是一名多用.运算符也可以重载.实际上,我们已经不知不觉中使用了运算符重载.如,大家都习惯用加法运算符"+"对整数.浮点数进行加法运算 5+8.5.8+3.67

2019.03.02

不知不觉在这边也是一年多了,今年年会上还感叹着时间过得快.这一年多,不知道是自己真正沉淀了,还是说压根没往前学习过什么东西,就像我之前记录里提到的,感觉在原地踏步.技术方面做的东西反而越来越少,每次老大会问"你最近都干了哪些项目,我在周报里没看你做了什么" 每当这个时候,我总是无言以对,每天感觉自己不停的在忙,但具体忙了哪些事情,我却说不很有代表性的工作.是自己一直在处理问题吗?有这一方面原因---工作的桌面问题虽然有单独人员负责,但一个人处理不过来的话正常我也应该去帮忙处理问题,那这

记2019/03/05的一次面试(性能测试)

今天约了一个面试,是某保险的外包性能岗.我第一次接触外包,一个HR联系了我,然后我问了一下具体JD,回复是:[一年以上性能测试经验,熟悉LR应用,最好是男生]. 我之前主要做的是功能,还有部分接口.性能测试这一块没有严格意义上的独立项目经验,都是公司偶尔的接口性能需求,而且我还没有服务器/数据库的权限,很多东西都看不到.再加上我认为以后的软件测试行业自动化测试应当是基础技能,而性能/安全等技能则为专项技能,想趁着自己还能折腾,走一段技术这条路在考虑管理的事情.于是乎,毅然决然的离职了. 桂林路下