在switch语句中,默认总是先运行

时间:2019-07-16 15:12:45

标签: javascript switch-statement

当我使用break时,预期的结果是在找到公司名称后停止执行开关,但是无论如何默认情况下先运行,然后删除从case创建的链接。我不明白原因。

预期结果:   1)如果:var company = Companyname1   2)link = id =“ exact_link” href =“ https://start.exactonline.nl/docs/MenuPortal.aspx?Division=1” target =“ _ blank” class =“ w3-button w3-red“>精确NL

结果:  1)如果:var company = Companyname1  2)link = id =“ exact_link” target =“ _ blank” onclick =“ function(){alert(”确切地说没有客户“);}” class =“ w3-button w3-red”>精确NL >

var companyMP = document.getElementsByClassName("_3vY7v2kjExcEpZ9m7ESYyW _3YWV3pFR0CN67Wsv40gDNn _3UGVE08RTYb-pG_1EHBWV1 y1g82IMXa_-RDPNXPcDPm");
 for (var i = 0; i < companyMP.length; i++) {
  var alertnocompany = function(){ alert("No client in Exact"); }
  var company = companyMP[i].innerText;
  switch (String(company)) {
   case "Companyname1":
    document.getElementById('exact_link').href = 'https://start.exactonline.nl/docs/MenuPortal.aspx?_Division_=1';
    break;
   case "Companyname2":
     document.getElementById('exact_link').href = 'https://start.exactonline.nl/docs/MenuPortal.aspx?_Division_=2';
     break;
   case "Companyname3":
     document.getElementById('exact_link').href = 'https://start.exactonline.nl/docs/MenuPortal.aspx?_Division_=3';
     break;
   case "Companyname4":
     document.getElementById('exact_link').href = 'https://start.exactonline.nl/docs/MenuPortal.aspx?_Division_=4';
     break;
   case "Companyname5":
     document.getElementById('exact_link').href = 'https://start.exactonline.nl/docs/MenuPortal.aspx?_Division_=5';
     break;
   default:
     document.getElementById('exact_link').onclick = alertnocompany;
     document.getElementById('exact_link').removeAttribute("href");  
     break;  
  }
 }
.w3-button {width:200px;}
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<a id="exact_link" href="" target="_blank" class="w3-button w3-red">Открыть в Exact NL</a>

0 个答案:

没有答案