Javascript 用于选择DOM元素的非特定JS
我通过cors从AJAX得到一个HTML响应,响应是一个表。每个类别都有其标题和子元素。书名差别很大,将来可能会改变。每个标题中的子元素几乎每天都在变化,但DOM结构没有变化 有没有办法摆脱这个if语句,用非元素特定的代码替换它?选择我不知道的DOM元素的某种方法 相关JSJavascript 用于选择DOM元素的非特定JS,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,我通过cors从AJAX得到一个HTML响应,响应是一个表。每个类别都有其标题和子元素。书名差别很大,将来可能会改变。每个标题中的子元素几乎每天都在变化,但DOM结构没有变化 有没有办法摆脱这个if语句,用非元素特定的代码替换它?选择我不知道的DOM元素的某种方法 相关JS classifiedFilter: function( response ) { var Classified = { ClaAdministrative: [
classifiedFilter: function( response ) {
var Classified = {
ClaAdministrative: [],
Paraeducator: [],
Clerical: [],
Custodial: [],
NonRep: [],
Maintenance: [],
ClaSubstitute: [],
Coaching: []
},
response = $(response).find("table tbody tr td").html();
$(response).find("#isHeadType").remove();
$(response).find("font:contains(Open to all)").parent().parent().remove();
//Filter each span title and classify sub items
$(response).find("span").parents("tr").each( function() {
//Find categories and separate by class
var rowtext = $(this).find("span").text(),
position = "";
position = rowtext.replace(/-/gi, "").replace(/\s/g, "");
$(this).nextAll("tr").addClass(position);
//Push content into Classified
((position === "Administrative") ? $(this).nextUntil(".Paraeducator").each( function() {
Classified.ClaAdministrative.push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
}) :
((position === "Paraeducator") ? $(this).nextUntil(".Clerical").each( function() {
Classified.Paraeducator.push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
}) :
((position === "Clerical") ? $(this).nextUntil(".Custodial").each( function() {
Classified.Clerical.push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
}) :
((position === "Custodial") ? $(this).nextUntil(".NonRep").each( function() {
Classified.Custodial.push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
}) :
((position === "NonRep") ? $(this).nextUntil(".Maintenance").each( function() {
Classified.NonRep.push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
}) :
((position === "Maintenance") ? $(this).nextUntil(".Substitute").each( function() {
Classified.Maintenance.push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
}) :
((position === "Substitute") ? $(this).nextUntil(".Coaching").each( function() {
Classified.ClaSubstitute.push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
}) :
((position === "Coaching") ? $(this).nextAll().each( function() {
Classified.Coaching.push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
}) : [] ) ) ) ) ) ) ) );
});
//remove title from array
Certificated.Elementary.shift();
Certificated.MiddleSchool.shift();
Certificated.HighSchool.shift();
Certificated.K12.shift();
Certificated.Substitute.shift();
}
classifiedFilter:功能(响应){
分类风险值={
行政管理:[],
副教育家:[],
文书:[],
保管:【】,
不可撤销:[],
维护:[],
替换:[],
辅导:[]
},
response=$(response.find(“table tbody tr td”).html();
$(响应)。查找(#isHeadType”).remove();
$(响应).find(“字体:包含(对所有人开放)”).parent().parent().remove();
//筛选每个范围标题并对子项进行分类
$(响应)。查找(“span”).parents(“tr”).each(函数(){
//查找类别并按类分开
var rowtext=$(this).find(“span”).text(),
位置=”;
位置=行文本。替换(/-/gi,“”)。替换(/\s/g,“”);
$(此).nextAll(“tr”).addClass(位置);
//将内容分类
((位置==“管理”)?$(此).nextUntil(“.Paraeducator”)。每个(函数(){
Classified.cladministrative.push($.trim(“+$(this.html()+));
}) :
((职位==“助理教育家”)?$(此).nextUntil(“.Clerical”).each(函数(){
Classified.Paraeducator.push($.trim(“+$(this.html()+));
}) :
((职位==“文书”)?$(此).nextUntil(“.Custodial”)。每个(功能(){
Classified.Clerical.push($.trim(“+$(this.html()+”));
}) :
((position==“Custodial”)?$(this).nextUntil(“.NonRep”)。每个(函数(){
Classified.Custodial.push($.trim(“+$(this.html()+”));
}) :
((position==“NonRep”)?$(this).nextUntil(“.Maintenance”)。每个(函数(){
Classified.NonRep.push($.trim(“+$(this.html()+”));
}) :
((位置==“维护”)?$(此).nextUntil(.Substitute”).each(函数(){
Classified.Maintenance.push($.trim(“+$(this.html()+));
}) :
((职位==“替换”)?$(此).nextUntil(“.Coaching”)。每个(功能(){
Classified.claplacement.push($.trim(“+$(this.html()+));
}) :
((位置==“辅导”)?$(this.nextAll().each(函数)(){
Classified.Coaching.push($.trim(“+$(this.html()+));
}) : [] ) ) ) ) ) ) ) );
});
//从数组中删除标题
认证的。基本的。移位();
证书。中学。班次();
持证。高中。轮班();
已认证。K12.shift();
认证。替换。移位();
}
摘要HTML获取响应(为视觉辅助添加了空格)
外部职位:对所有申请人开放。
行政的
副教育家
办公室文书工作的
非代表
代替
我认为,您可以在find函数中使用CSS类名而不是标记名。这似乎相当于您当前代码的核心:
$(this).each( function() {
Classified[position].push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
});
$(this).each(函数(){
分类[位置].push($.trim(“+$(this).html()+”);
});
[编辑]我看不出您在哪里声明数组,您实际上可能需要:
$(this).each( function() {
Classified[position]=Classified[position]||[];
Classified[position].push( $.trim( "<tr>" + $(this).html() + "</tr>" ) );
});
$(this).each(函数(){
分类[职位]=分类[职位]| |[];
分类[位置].push($.trim(“+$(this).html()+”);
});
[更新]根据注释,此循环在到达范围时更新位置:
var position = "";
$(response).find("tr").each( function() {
var currentTR=$(this);
// catch span if any
var rowtext = currentTR.find("span").text().replace(/-/gi, "").replace(/\s/g, "");
if (rowtext) {
position = rowtext;
Classified[position]=[];
}
// Collect rows
Classified[position].push( "<tr>" + $.trim($(this).html()) + "</tr>" );
});
var位置=”;
$(响应).find(“tr”).each(函数(){
var currentTR=$(此值);
//捕获跨度(如有)
var rowtext=currentTR.find(“span”).text().replace(/-/gi,”).replace(/\s/g,”);
如果(行文本){
位置=行文本;
分类[职位]=[];
}
//收集行
分类[位置]。推(“+$。修剪($(t
var position = "";
$(response).find("tr").each( function() {
var currentTR=$(this);
// catch span if any
var rowtext = currentTR.find("span").text().replace(/-/gi, "").replace(/\s/g, "");
if (rowtext) {
position = rowtext;
Classified[position]=[];
}
// Collect rows
Classified[position].push( "<tr>" + $.trim($(this).html()) + "</tr>" );
});