jquery insertAfter for ie8

旧街凉风 提交于 2020-01-05 06:46:50

问题


        var attachmentDeleteMainModal = $('#attachment-deletion');
        var attachmentDeleteMainModalClone = attachmentDeleteMainModal.clone();
        attachmentDeleteMainModalClone.attr('id', 'attachment-deletion-'+'main');
        attachmentDeleteMainModalClone.insertAfter('#attachment-deletion');

This method adds my new selector to the DOM in Chrome, but does not work in ie8, this is all I tested so far

append instead of insertAfter does not create the desired selector in either browser. But in ie8 it does not create anything at all

what is the solution to this? any insight appreciated


回答1:


You could try with

 $('#attachment-deletion').after(attachmentDeleteMainModalClone);



回答2:


In IE8 I had an exception in $rows.insertAfter($(elem)) line, so I fixed it with this solution:

            var $rows = $("tr", $(parsedXML));
            try {
                $rows.insertAfter($(elem));
            } catch (error) {
                // we got <table><tbody><tr... from the server and need to paste only all <tr> from it after our tr in $(elem)
                var divData = $("table", $(parsedXML));
                var divTmp = document.createElement("div");
                divTmp.innerHTML = divData[0].xml;
                var children = divTmp.firstChild.firstChild;
                var fragment = document.createDocumentFragment();
                var current = children.firstChild;

                while (current) {
                    fragment.appendChild(current.cloneNode(true));
                    current = current.nextSibling;
                }

                elem.parentNode.insertBefore(fragment, elem.nextSibling);
            }

Sorry, it is not so clean with ".firstChild.firstChild", but worked for me. We got <table><tbody><tr... from the server and need to paste only all <tr> from it after our tr in $(elem)



来源:https://stackoverflow.com/questions/17157801/jquery-insertafter-for-ie8

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