语法错误:意外的标识符'modal_html'。预期为“:”

时间:2019-06-01 13:20:27

标签: javascript

SyntaxError:意外标识符'modal_html'。预期为“:” 在属性名称“ var”之后。 (匿名函数)— modal.self-

Helpers = window.Helpers || {}
Helpers.Bootstrap = Helpers.Bootstrap || {}

Helpers.Bootstrap.Modal = {

  var modal_html = `
  <div class="modal fade" id="AlertModal" tabindex="-1" role="dialog" aria-labelledby="AlertModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">

        <div class="modal-header">
          <h5 class="modal-title" id="AlertModalLabel">${title}</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
          </button>
        </div>

        <div class="modal-body">
          ${message}
        </div>
        <div class="modal-footer">
          ${ button_html( button1 )}
          ${ button_html( button2 )}
        </div>

    </div>
  </div>
  `;

  init(title, message, button1, button2, existingelementid) {
    if (b === undefined) {
      Helpers.Bootstrap.Modal.closee(existingelementid)
    }
    modal_html
  }

  close(elementid) {
    $( elementid + " .close").click();
  }

  button_html(button_name) {
    var button_cancel = "<button type="button" class="btn btn-outline-danger" data-dismiss="modal">Cancel</button>";
    var button_save   = "<button type="submit" class="btn btn-outline-primary spinner" >Save</button>";
    var button_ok     = "<button type="button" class="btn btn-outline-primary" data-dismiss="modal">Ok</button>";
    var button_close  = "<button type="button" class="btn btn-outline-primary" data-dismiss="modal">Close</button>";

    switch(button_name) {
      case "cancel":
        button_cancel;
        break;
      case "save":
        button_save;
        break;
      case "ok":
        button_ok;
        break;
      case "close":
        button_close;
        break;
      default:
        button_name;
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您在这里做错了很多事情:

  • 以分号Helpers = window.Helpers || {}结束作业Helpers.Bootstrap = Helpers.Bootstrap || {};(正因为如此!)
  • 我认为Helpers.Bootstrap.Modal.closee(existingelementid)应该是.close,而不是.closee
  • modal_html中的html最后缺少结尾的</div>标签
  • 您无法像使用var一样在对象内对var XXX = something;进行贴花处理 提示您只需执行以下操作:XXX : something, <-(请注意最后的,
  • 在您的函数init中,它只是随机地说modal_html(没有分配或其他任何内容)
  • button_html中的整个代码简直是胡言乱语... ofc var button_cancel = "<button type="不是真正的任务,因此继续 button " class="会破坏一切! --->您不能使用"来分隔""内的定界符
  • 您永远不会从switch返回任何信息(至少我认为您想这样做)
  • 还有更多,但我建议先解决此问题,也许您的原始错误会神奇地消失;)

Helpers = window.Helpers || {}; /* <------ */
Helpers.Bootstrap = Helpers.Bootstrap || {}; /* <------- */

Helpers.Bootstrap.Modal = {

  modal_html : `
  <div class="modal fade" id="AlertModal" tabindex="-1" role="dialog"               aria-labelledby="AlertModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">

        <div class="modal-header">
          <h5 class="modal-title" id="AlertModalLabel"></h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
          </button>
        </div>

        <div class="modal-body">
          
        </div>
        <div class="modal-footer">
        </div>

    </div>
  </div>
  </div> <!-- <--------- -->
  `,


  
  
  button_html(button_name) {
  /*
    var button_cancel = "<button type="
    button " class="
    btn btn - outline - danger " data-dismiss="
    modal ">Cancel</button>";
    var button_save = "<button type="
    submit " class="
    btn btn - outline - primary spinner " >Save</button>";
    var button_ok = "<button type="
    button " class="
    btn btn - outline - primary " data-dismiss="
    modal ">Ok</button>";
    var button_close = "<button type="
    button " class="
    btn btn - outline - primary " data-dismiss="
    modal ">Close</button>";

    switch (button_name) {
      case "cancel":
        button_cancel;
        break;
      case "save":
        button_save;
        break;
      case "ok":
        button_ok;
        break;
      case "close":
        button_close;
        break;
      default:
        button_name;
    }
    
    THE ABOVE CODE IN THIS COMMENT IS ALL GIBBERISH AND IS NOT CORRECT */
  }

}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">