哈哈强大的 查找 网页元素 函数
// 1 相同,2 包含 ,3不包含 4 不等于 21 上级元素相同,22 上级元素包含 ,23 上级元素不包含 24 上级元素不等于 -1 下级元素相同,-2 下级元素包含 ,-3 下级元素不包含 -4 下级元素不等于, -5 没有下级元素
//31 上兄弟元素相同,32 上兄弟元素包含 ,33上兄弟元素不包含 34 兄弟上元素不等于,35 没有上兄弟元素,41 下兄弟元素相同,42 下兄弟元素包含 ,43下兄弟元素不包含 44 下兄弟元素不等于 ,45 没有下兄弟元素
//ARpmatchReg为trim时表示trim匹配 其它为正则
//使用object主要是不想让this成为全局变量 MEO.getElementsByAttrs(“a”,“title”,“PDF”,2,"",“a”,“href”,"/pdf/",2,"")[0] 如果要不包含标签文字(用43等) tag应该定义为’’
//MEO.getElementsByAttrs("p","outerHTML","<p>",2,"","div","id","a",21,/\d/g,"a","outerHTML","<a",5,"")
//MEO.getElementsByAttrs("p","outerHTML","<p",2,"","","outerHTML","\<span",45,"")
var MEO = {
getElementsByAttrs:function getElementsByAttrs(tag0,attr0,value0,Ifinclude0,ARpmatchReg0,tag1,attr1,value1,Ifinclude1,ARpmatchReg1)//http://booksdescr.org/ads.php?md5=2d7ee23074174468b8ec1a81667acbb8 x=getElementsByAttrs("a","href","get.php",2,"","a","innerText","GET",-1,'')[0]
{
//tag=tag.toUpperCase();
if (arguments.length%5 == 0 && arguments[5*( arguments.length/5-1) + 3].constructor==Number)
{
var aElements=document.getElementsByTagName(tag0);
var aEles=[];
var KeyNum = arguments.length/5;
var atrArr=["innerText", "textContent", "innerHTML", "outerText", "outerHTML", "selfinnerText"];
for(var i=0;i<aElements.length;i++)
{
//var ifThisE = true;
this['IfMatch'+ (KeyNum-1)] = false;
for(var j=0;j<KeyNum; j++)
{
this['IfMatch'+j] = false;
this['attr'+j+ 'V']=null;
this['AElem'+j]=null;
var argu0 = arguments[5*j + 0];
var argu1 = arguments[5*j + 1];
var argu2 = arguments[5*j + 2];
var argu3 = arguments[5*j + 3];
var argu4 = arguments[5*j + 4];
var ifNoE = [-5,25,35,45].includes(argu3);
if ([1, 2, 3, 4].includes(argu3) ) {this['AElem'+j] = aElements[i];} else
if ([21, 22, 23, 24,25].includes(argu3) && aElements[i].parentElement!=null ) {this['AElem'+j] = aElements[i].parentElement;} else
if ([31, 32, 33, 34,35].includes(argu3) && aElements[i].previousElementSibling!=null ) {this['AElem'+j] = aElements[i].previousElementSibling;} else
if ([41, 42, 43, 44,45].includes(argu3) && aElements[i].nextElementSibling!=null ) {this['AElem'+j] = aElements[i].nextElementSibling;} else
if ([-1, -2, -3, -4,-5].includes(argu3) && aElements[i].firstElementChild!=null ) {this['AElem'+j] = aElements[i].firstElementChild;}
//console.log('argu3: '+argu3+ '---ifNoE: '+ifNoE); //
if (!ifNoE)
{
if (!this['AElem'+j] ) //如果没有该元素,直接退出,比如当ifinclude为-1时没有子元素等的时候其为空
{
//ifThisE = false;
break;
}
if (atrArr.indexOf(argu1)>=0 )
{
if (argu1.toLowerCase() !='selfinnerText'.toLowerCase() )
{
//console.log('this["AElem"+j]:' + this["AElem"+j].outerHTML + 'argu1:' + argu1);
this['attr'+j+ 'V'] = eval( 'this["AElem"+j].'+ argu1) ;
} else
{
this['attr'+j+ 'V'] = GetEleSelfTxt(this['AElem'+j]);
}
} else
{
this['attr'+j+ 'V'] = this['AElem'+j].getAttribute(argu1);
};
if (!this['attr'+j+ 'V'] ) //如果没有该元素值,直接退出,要不然为undefined时下面替换会出错
{
//ifThisE = false;
break;
}
if (argu4 =='trim' )
{
this['attr'+j+ 'V'] = (this['attr'+j+ 'V']).trim();
} else
if (argu4!='' )
{
this['attr'+j+ 'V'] = this['attr'+j+ 'V'].replace(argu4,''); //x='df'+ '\f' + '\n'+ '\r' +'\t' +'\v'+'hj'; x.replace(/\s/g); 比如替换任何不可见字符,包括空格、制表符、换页符等等 等价于[ \f\n\r\t\v]
}
//console.log(i + ':--' +j+': ---attr'+j+ 'V:'+this['attr'+j+ 'V'] +'---value:'+ argu2);
if (this['AElem'+j].tagName.toLowerCase() == argu0.toLowerCase() || argu0=='')
{
if ( (argu3.toString().slice(-1)=='1' ) && (this['attr'+j+ 'V']!=null) && (this['attr'+j+ 'V']==(argu2)) )
{
this['IfMatch'+j]=true;
}else
if ( (argu3.toString().slice(-1)=='2') && (this['attr'+j+ 'V']!=null) && (this['attr'+j+ 'V'].includes(argu2)) )
{
this['IfMatch'+j]=true;
} else
if( (argu3.toString().slice(-1)=='3') && (this['attr'+j+ 'V']!=null) && !(this['attr'+j+ 'V'].includes(argu2)) )
{
this['IfMatch'+j]=true;
} else
if( (argu3.toString().slice(-1)=='4') && (this['attr'+j+ 'V']!=null) && (this['attr'+j+ 'V']!=(argu2)) )
{
this['IfMatch'+j]=true;
}
}
if (!this['IfMatch'+j])
{
//ifThisE = false;
break;
}
//console.log(i + ':--' +j+': ---attr'+j+ 'V:'+this['attr'+j+ 'V'] +'---value'+ j+':'+ argu2 +'this["IfMatch"+j]:'+this['IfMatch'+j] +'---ifThisE:'+ ifThisE +' --this["AElem"+j]outerHTML: '+ this['AElem'+j].outerHTML);
//console.log('argu3.toString().slice(-1)=="3"'+ argu3.toString().slice(-1) + '---' +this['attr'+j+ 'V']+ '---' + (argu2) + this['attr'+j+ 'V'].includes(argu2));
} else// !ifNoE
{
if (this['AElem'+j] ) //如果有该元素,说明不符合条件
{
//ifThisE = false;
break;
} else {this['IfMatch'+j]=true;}//如果没有该元素,说明符合条件
}
//if (this['AElem'+j] ) {console.log(i + ':--' +j+': ---attr'+j+ 'V:'+this['attr'+j+ 'V'] +'---value'+ j+':'+ argu2 +'this["IfMatch"+j]:'+this['IfMatch'+j] +'---ifThisE:'+ ifThisE +' --this["AElem"+j]outerHTML: '+ this['AElem'+j].outerHTML); } else {console.log(i + ':--' +j+': ---attr'+j+ 'V:'+this['attr'+j+ 'V'] +'---value'+ j+':'+ argu2 +'this["IfMatch"+j]:'+this['IfMatch'+j]+'---ifThisE:'+ ifThisE +' --this["AElem"+j]outerHTML: 没有' ); }
} //j<KeyNum
if( this['IfMatch'+ (KeyNum-1)] )//if( ifThisE )
{aEles.push( aElements[i] );}
}//i<aElements.length
return aEles;
} else //arguments.length%5
{
alert('函数参数输入错误');
}
}
}
调用: MEO.getElementsByAttrs(‘h3’,‘class’,‘t1’,3,’’,‘h3’,‘class’,‘t’,2,‘trim’,‘h3’,‘class’,‘t’,1,/4/g)
注:要想兼容IE,可将includes换成indexof;
使用MEO主要是不愿意this成为全局变量。
getElementsByAttrs 当然可以用简单的字符串啦,比如 $代替以简化代码 var MEO = { $:function x()…}