dom get selector

function realsQuery(element) {
if(!element){
return "";
}
var currentQuery="";
if (element.id !== "") {
currentQuery='#' + element.id;
}else{
currentQuery= getSimpleQuery(element,currentQuery);
console.log("simpleQuery:"+currentQuery);
/**currentQuery="";
currentQuery= getDetailsQuery(element,currentQuery);
console.log("detailsQuery:"+currentQuery);
currentQuery="";
currentQuery= getTypeQuery(element,currentQuery);
console.log("TypeQuery:"+currentQuery);**/
}
return currentQuery;


};


function getSimpleQuery(element,currentQuery){
var siblings = element.parentNode.childNodes;
currentQuery=getThisQuery(element,siblings);
currentQuery=getParentQuery(element,currentQuery);
return currentQuery;
}
function getDetailsQuery(element,currentQuery){
var siblings = element.parentNode.childNodes;
currentQuery=getThisQueryD(element,siblings);
currentQuery=getParentQueryD(element,currentQuery);
return currentQuery;
}

function getTypeQuery(element,currentQuery){
var siblings = element.parentNode.childNodes;
currentQuery=getThisQueryT(element,siblings);
currentQuery=getParentQueryT(element,currentQuery);
return currentQuery;
}


function getParentQuery(element,queryName){
var that=element.parentNode;
if(that!=null){
if(that.tagName.toLowerCase()=="body"){
queryName="body > "+queryName;
}else{
var iiid=(that.id||"").replace(/(^\s*)|(\s*$)/g, "");

if(iiid){
queryName="#"+iiid+" > "+queryName;
}else{
var siblings=null
if(that.parentNode){
siblings=that.parentNode.childNodes;
}else{
siblings=[that];
}
var currentName=getThisQuery(that,siblings);
queryName=currentName+" > "+queryName;
if(that!=null){
queryName=getParentQuery(that,queryName);
}
}

}


}
return queryName;

}
function getThisQuery(element,siblings){
var newsiblings=[];
var index=0;
for(var i=0;i<siblings.length;i++){
if(siblings[i].tagName&&siblings[i].tagName.toLowerCase()==element.tagName.toLowerCase()){
newsiblings[index++]=siblings[i];
}

}

var currentQuery="";
if(newsiblings.length==1){
var classStr=element.getAttribute("class");
classStr=(classStr||"").replace(/(^\s*)|(\s*$)/g, "").replace(/\s{2,}/g," ");
/**if(classStr){
currentQuery=element.tagName.toLowerCase();
var strs=classStr.split(" ");
for(var i=0;i<strs.length;i++){
currentQuery+="."+strs[i];
}

}else{
currentQuery=element.tagName.toLowerCase();
}**/
currentQuery=element.tagName.toLowerCase();

}
else{


var arrayList=[];
var classStr=element.getAttribute("class");
classStr=(classStr||"").replace(/(^\s*)|(\s*$)/g, "").replace(/\s{2,}/g," ");
var currentClass=[];
if(classStr){
currentQuery=element.tagName.toLowerCase();
var once=true;
for(var i=0;i<newsiblings.length;i++){
if(newsiblings[i].tagName&&newsiblings[i].tagName.toLowerCase()!=element.tagName.toLowerCase()){
continue;
}
if(!newsiblings[i].tagName){
continue;
}
var hehehe=newsiblings[i].getAttribute("class")||"";
hehehe=hehehe.replace(/(^\s*)|(\s*$)/g, "").replace(/\s{2,}/g," ");
var strs=hehehe.split(" ");
if(once){
currentClass=strs;
once=false;
}else{
currentClass=currentClass.filter(v => strs.includes(v))
}
}

var strs=classStr.split(" ");
var appendSonClss="";
for(var i=0;i<strs.length;i++){
if(currentClass.indexOf(strs[i])>=0){
continue;
}
appendSonClss+="."+strs[i];
}
if(appendSonClss){
currentQuery+=appendSonClss;
}else{

var ix = 0;//在nodelist中的位置,且每次点击初始化
for(var i=0;i<siblings.length;i++){
var sibling = siblings[i];
//如果这个元素是siblings数组中的元素,则执行递归操作
if (sibling == element) {
//var parentQuery=realsQuery(element.parentNode);
ix++;
currentQuery=element.tagName.toLowerCase() + ':nth-child(' + ix+ ')';
//如果不符合,判断是否是element元素,并且是否是相同元素,如果是相同的就开始累加
} else if (sibling.nodeType == 1) {
ix++;
}
}
}

}else{
var ix = 0;//在nodelist中的位置,且每次点击初始化
for(var i=0;i<siblings.length;i++){
var sibling = siblings[i];
//如果这个元素是siblings数组中的元素,则执行递归操作
if (sibling == element) {
//var parentQuery=realsQuery(element.parentNode);
ix++;
currentQuery=element.tagName.toLowerCase() + ':nth-child(' + ix+ ')';
//如果不符合,判断是否是element元素,并且是否是相同元素,如果是相同的就开始累加
} else if (sibling.nodeType == 1) {
ix++;
}
}

}



}
return currentQuery;

}


function getParentQueryT(element,queryName){
var that=element.parentNode;
if(that!=null){
if(that.tagName.toLowerCase()=="body"){
queryName="body > "+queryName;
}else{
var iiid=(that.id||"").replace(/(^\s*)|(\s*$)/g, "");
if(iiid){
queryName="#"+iiid+" > "+queryName;
}else{
var siblings=null
if(that.parentNode){
siblings=that.parentNode.childNodes;
}else{
siblings=[that];
}
var currentName=getThisQueryT(that,siblings);
queryName=currentName+" > "+queryName;
if(that!=null){
queryName=getParentQueryT(that,queryName);
}
}

}


}
return queryName;

}
function getThisQueryT(element,siblings){
var newsiblings=[];
var index=0;
for(var i=0;i<siblings.length;i++){
if(siblings[i].tagName&&siblings[i].tagName.toLowerCase()==element.tagName.toLowerCase()){
newsiblings[index++]=siblings[i];
}

}
siblings=newsiblings;
var currentQuery="";
if(newsiblings.length==1){
var classStr=element.getAttribute("class");
classStr=(classStr||"").replace(/(^\s*)|(\s*$)/g, "").replace(/\s{2,}/g," ");

/**if(classStr){
currentQuery=element.tagName.toLowerCase();
var strs=classStr.split(" ");
for(var i=0;i<strs.length;i++){
currentQuery+="."+strs[i];
}

}else{
currentQuery=element.tagName.toLowerCase();
}**/
currentQuery=element.tagName.toLowerCase();

}
else{


var arrayList=[];
var classStr=element.getAttribute("class");
classStr=(classStr||"").replace(/(^\s*)|(\s*$)/g, "").replace(/\s{2,}/g," ");
var currentClass=[];
if(classStr){
currentQuery=element.tagName.toLowerCase();
var once=true;
for(var i=0;i<newsiblings.length;i++){
if(newsiblings[i].tagName&&newsiblings[i].tagName.toLowerCase()!=element.tagName.toLowerCase()){
continue;
}
if(!newsiblings[i].tagName){
continue;
}
var hehehe=newsiblings[i].getAttribute("class")||"";
hehehe=hehehe.replace(/(^\s*)|(\s*$)/g, "").replace(/\s{2,}/g," ");
var strs=hehehe.split(" ");
if(once){
currentClass=strs;
once=false;
}else{
currentClass=currentClass.filter(v => strs.includes(v))
}
}

var strs=classStr.split(" ");
var appendSonClss="";
for(var i=0;i<strs.length;i++){
if(currentClass.indexOf(strs[i])>=0){
continue;
}
appendSonClss+="."+strs[i];
}
if(appendSonClss){
currentQuery+=appendSonClss;
}else{

var ix = 0;//在nodelist中的位置,且每次点击初始化
for(var i=0;i<siblings.length;i++){
var sibling = siblings[i];
//如果这个元素是siblings数组中的元素,则执行递归操作
if (sibling == element) {
//var parentQuery=realsQuery(element.parentNode);
ix++;
currentQuery=element.tagName.toLowerCase() + ':nth-of-type(' + ix+ ')';
//如果不符合,判断是否是element元素,并且是否是相同元素,如果是相同的就开始累加
} else if (sibling.nodeType == 1 && sibling.tagName == element.tagName) {
ix++;
}
}
}

}else{
var ix = 0;//在nodelist中的位置,且每次点击初始化
for(var i=0;i<siblings.length;i++){
var sibling = siblings[i];
//如果这个元素是siblings数组中的元素,则执行递归操作
if (sibling == element) {
//var parentQuery=realsQuery(element.parentNode);
ix++;
currentQuery=element.tagName.toLowerCase() + ':nth-of-type(' + ix+ ')';
//如果不符合,判断是否是element元素,并且是否是相同元素,如果是相同的就开始累加
} else if (sibling.nodeType == 1 && sibling.tagName == element.tagName) {
ix++;
}
}

}



}
return currentQuery;

}


function getParentQueryD(element,queryName){
var that=element.parentNode;
if(that!=null){
if(that.tagName.toLowerCase()=="body"){
queryName="body > "+queryName;
}else{
var iiid=(that.id||"").replace(/(^\s*)|(\s*$)/g, "");


if(iiid){
queryName="#"+iiid+" > "+queryName;
}else{
var siblings=null
if(that.parentNode){
siblings=that.parentNode.childNodes;
}else{
siblings=[that];
}
var currentName=getThisQueryD(that,siblings);
queryName=currentName+" > "+queryName;
if(that!=null){
queryName=getParentQueryD(that,queryName);
}
}

}


}
return queryName;

}
function getThisQueryD(element,siblings){
var newsiblings=[];
var index=0;
for(var i=0;i<siblings.length;i++){
if(siblings[i].tagName&&siblings[i].tagName.toLowerCase()==element.tagName.toLowerCase()){
newsiblings[index++]=siblings[i];
}

}
siblings=newsiblings;
var currentQuery="";
if(newsiblings.length==1){
var classStr=element.getAttribute("class");
classStr=(classStr||"").replace(/(^\s*)|(\s*$)/g, "").replace(/\s{2,}/g," ");

if(classStr){
currentQuery=element.tagName.toLowerCase();
var strs=classStr.split(" ");
for(var i=0;i<strs.length;i++){
currentQuery+="."+strs[i];
}

}else{
currentQuery=element.tagName.toLowerCase();
}/****/
//currentQuery=element.tagName.toLowerCase();

}
else{


var arrayList=[];
var classStr=element.getAttribute("class");
classStr=(classStr||"").replace(/(^\s*)|(\s*$)/g, "").replace(/\s{2,}/g," ");
var currentClass=[];
if(classStr){
currentQuery=element.tagName.toLowerCase();
var once=true;
for(var i=0;i<newsiblings.length;i++){
if(newsiblings[i].tagName&&newsiblings[i].tagName.toLowerCase()!=element.tagName.toLowerCase()){
continue;
}
if(!newsiblings[i].tagName){
continue;
}
var hehehe=newsiblings[i].getAttribute("class")||"";
hehehe=hehehe.replace(/(^\s*)|(\s*$)/g, "").replace(/\s{2,}/g," ");
var strs=hehehe.split(" ");
if(once){
currentClass=strs;
once=false;
}else{
currentClass=currentClass.filter(v => strs.includes(v))
}
}

var strs=classStr.split(" ");
var appendSonClss="";
for(var i=0;i<strs.length;i++){
if(currentClass.indexOf(strs[i])>=0){
continue;
}
appendSonClss+="."+strs[i];
}
if(appendSonClss){
currentQuery+=appendSonClss;
}else{

var ix = 0;//在nodelist中的位置,且每次点击初始化
for(var i=0;i<siblings.length;i++){
var sibling = siblings[i];
//如果这个元素是siblings数组中的元素,则执行递归操作
if (sibling == element) {
//var parentQuery=realsQuery(element.parentNode);
ix++;
currentQuery=element.tagName.toLowerCase() + ':nth-of-type(' + ix+ ')';
//如果不符合,判断是否是element元素,并且是否是相同元素,如果是相同的就开始累加
} else if (sibling.nodeType == 1 && sibling.tagName == element.tagName) {
ix++;
}
}
}

}else{
var ix = 0;//在nodelist中的位置,且每次点击初始化
for(var i=0;i<siblings.length;i++){
var sibling = siblings[i];
//如果这个元素是siblings数组中的元素,则执行递归操作
if (sibling == element) {
//var parentQuery=realsQuery(element.parentNode);
ix++;
currentQuery=element.tagName.toLowerCase() + ':nth-of-type(' + ix+ ')';
//如果不符合,判断是否是element元素,并且是否是相同元素,如果是相同的就开始累加
} else if (sibling.nodeType == 1 && sibling.tagName == element.tagName) {
ix++;
}
}

}



}
return currentQuery;

}

 

posted @ 2019-03-30 16:18  吃饭了吗  阅读(158)  评论(0编辑  收藏  举报