[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, ornull
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)如果你需要找到所有的匹配元素,请使用q
ueryselectorall
。
真的是很方便了。实际案例在下面!
还值得一提的是,这个方法相比于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
上一篇: JS 只创建一个元素
下一篇: 纯css3实现的动画按钮的实例教程