题目:
我的解法:
js handlebars">js-doctype"><!DOCTYPE html>
js-tag"><js-title">html js-attribute">lang=js-value">"en">
js-tag"><js-title">head
<js-attribute">meta js-attribute">charset=js-value">"UTF-8">
js-tag"><js-title">meta js-attribute">name=js-value">"viewport" js-attribute">content=js-value">"width=device-width, initial-scale=1.0">
js-tag"><js-title">meta js-attribute">http-equiv=js-value">"X-UA-Compatible" js-attribute">content=js-value">"ie=edge">
js-tag"><js-title">title>Documentjs-tag"></js-title">title>
js-tag"></js-title">head>
js-tag"><js-title">body>
js-tag"><js-title">div js-attribute">id=js-value">"page">
js-tag"><js-title">div js-attribute">class=js-value">"content main">
js-tag"><js-title">div js-attribute">class=js-value">"refer">
js-tag"><js-title">ul>
js-tag"><js-title">li>click1js-tag"></js-title">li>
js-tag"><js-title">li>click2js-tag"></js-title">li>
js-tag"><js-title">li>click3js-tag"></js-title">li>
js-tag"></js-title">ul>
js-tag"></js-title">div>
js-tag"></js-title">div>
js-tag"></js-title">div>
js-tag"></js-title">body>
js-tag"><js-title">script>
js-keyword">var genCssSelector=js-function">js-keyword">functionjs-params">(){
js-comment">//you code here
js-keyword">var ele=js-built_in">arguments[js-number">0];
js-keyword">var parentEle=ele.parentNode;
js-keyword">var strGenCssSelector=ele.tagName.toLowerCase();
js-keyword">while(parentEle.nodeName.toLowerCase()!==js-string">'#document'){
js-keyword">if(parentEle.id!==js-string">''){
strGenCssSelector=js-string">'#'+parentEle.id+js-string">' '+strGenCssSelector;
parentEle=parentEle.parentNode;
js-keyword">continue;
}js-keyword">else js-keyword">if(parentEle.className!=js-string">''){
strGenCssSelector=js-string">' '+strGenCssSelector;
js-keyword">var classList=parentEle.classList||parentEle.className.split(js-string">' ');
js-keyword">for(js-keyword">var i=js-number">0;i<classList.length;i++){
strGenCssSelector=js-string">'.'+classList[i]+strGenCssSelector;
}
parentEle=parentEle.parentNode;
js-keyword">continue;
}js-keyword">else{
strGenCssSelector=parentEle.nodeName.toLowerCase()+js-string">' '+strGenCssSelector;
parentEle=parentEle.parentNode;
js-keyword">continue;
}
}
js-keyword">return strGenCssSelector;
}
document.addEventListener(js-string">'click',js-function">js-keyword">functionjs-params">(e){
js-comment">//点击li时,返回html body #page .content.main .refer ul li
alert(genCssSelector(e.target));
})
js-tag"></js-title">script>
js-tag"></js-title">html>