Error in IE11 when trying to show a bootstrap Modal via JavaScript

岁酱吖の 提交于 2020-01-05 11:58:09

问题


modal.modal('show'); is triggering an error in internet explorer while it is working in all other browsers The Error is Object doesn't support property or method 'modal'

Note:This modal is called by more than one button and it needs to be updated every time which is why I am doing the modal reset.

On a side note, why is it that I have to clone the modal twice to do a modal reset? Is there a better way to do the reset?

var original_model = "";
$(document).ready(function() {
    original_model = $('#my_modal_id').clone();
})

function show_modal(user_photo) {

    //reseting the modal
    $("#my_modal_id").remove();
    var myClone = original_model.clone();
    $("body").append(myClone);

    var modal = $("#my_modal_id");

    modal.find("#photo").attr("src", user_photo);

    // more code to update the modal ...

    // Does not work in IE11 : Error : Object doesn't support property or method 'modal'
    modal.modal('show');
}

回答1:


Try adding this code to your show_modal() function after the var modal declaration:

if ($.browser.version > 9){
    modal.removeClass('fade');
}

This would require the download of the jQuery migrate plugin




回答2:


In case you use compilation for your project (with webpack, for example), you might consider to install the current polyfill package, that provides ES5 compatibility

npm i --save core-js

or

yarn add core-js

Then to add at the top of your entry point of your project

import 'core-js'

At the moment core-js polyfill library is the easiest way to make Cross Browser Support



来源:https://stackoverflow.com/questions/25716805/error-in-ie11-when-trying-to-show-a-bootstrap-modal-via-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!