如果它们没有嵌套,我可以使用多个else if语句吗?

时间:2016-08-10 23:32:17

标签: javascript bookmarklet

我正在尝试创建一个bookmarklett并且可以通过删除所有其他if语句来实现这一点,但是如果/ else if / else就是这样......但不是所有这些...想知道他们是否应该嵌套?

if(document.documentElement.outerHTML.indexOf("leads/process") > -1 {
   alert("GOAL ID: leads");
} else if(document.documentElement.outerHTML.indexOf("apply-continue/submissions") > -1 {
   alert("GOAL ID: Apply Continue");
} else if(document.documentElement.outerHTML.indexOf("apply/submissions") > -1 {
   alert("GOAL ID: Apply");
} else if(document.documentElement.outerHTML.indexOf("answered-call/submissions") > -1 {
   alert("GOAL ID: Answered Call");
} else if(document.documentElement.outerHTML.indexOf("contact/submissions") > -1 {
   alert("GOAL ID: Contact");
} else if(document.documentElement.outerHTML.indexOf("contact-us/submissions") > -1 {
   alert("GOAL ID: Contact Us");
} else if(document.documentElement.outerHTML.indexOf("maintenance-request/submissions") > -1 {
   alert("GOAL ID: Maintenance Request");
} else if(document.documentElement.outerHTML.indexOf("online-application/submissions") > -1 {
   alert("GOAL ID: Online Application");
} else if(document.documentElement.outerHTML.indexOf("rent-now/submissions") > -1 {
   alert("GOAL ID: Rent Now");
} else if(document.documentElement.outerHTML.indexOf("request-info/submissions") > -1 {
   alert("GOAL ID: Request Info.");
} else if(document.documentElement.outerHTML.indexOf("suggestion-box/submissions") > -1 {
   alert("GOAL ID: Suggestion Box");
}  else if(document.documentElement.outerHTML.indexOf("tour/submissions") > -1 {
   alert("GOAL ID: Tour");
} else if(document.documentElement.outerHTML.indexOf("tour-request/submissions") > -1 {
   alert("GOAL ID: Tour Request");
} else {
   alert("CUSTOM GOAL ID: Goal ID used is not in the standard set.");
}

1 个答案:

答案 0 :(得分:1)

你不需要嵌套if语句,你所拥有的几乎没问题,除非你缺少if条件的结束括号,试试:

if(document.documentElement.outerHTML.indexOf("leads/process") > -1 ){
   alert("GOAL ID: leads");
} else if(document.documentElement.outerHTML.indexOf("apply-continue/submissions") > -1 ){
   alert("GOAL ID: Apply Continue");
} else if(document.documentElement.outerHTML.indexOf("apply/submissions") > -1 ){
   alert("GOAL ID: Apply");
} else if(document.documentElement.outerHTML.indexOf("answered-call/submissions") > -1 ){
   alert("GOAL ID: Answered Call");
} else if(document.documentElement.outerHTML.indexOf("contact/submissions") > -1 ){
   alert("GOAL ID: Contact");
} else if(document.documentElement.outerHTML.indexOf("contact-us/submissions") > -1 ){
   alert("GOAL ID: Contact Us");
} else if(document.documentElement.outerHTML.indexOf("maintenance-request/submissions") > -1 ){
   alert("GOAL ID: Maintenance Request");
} else if(document.documentElement.outerHTML.indexOf("online-application/submissions") > -1 ){
   alert("GOAL ID: Online Application");
} else if(document.documentElement.outerHTML.indexOf("rent-now/submissions") > -1 ){
   alert("GOAL ID: Rent Now");
} else if(document.documentElement.outerHTML.indexOf("request-info/submissions") > -1 ){
   alert("GOAL ID: Request Info.");
} else if(document.documentElement.outerHTML.indexOf("suggestion-box/submissions") > -1 ){
   alert("GOAL ID: Suggestion Box");
}  else if(document.documentElement.outerHTML.indexOf("tour/submissions") > -1 ){
   alert("GOAL ID: Tour");
} else if(document.documentElement.outerHTML.indexOf("tour-request/submissions") > -1 ){
   alert("GOAL ID: Tour Request");
} else {
   alert("CUSTOM GOAL ID: Goal ID used is not in the standard set.");
}