没有不值得去解决的问题,也没有不值得去学习的技术!

Solution When jQuery insertBefore in an iframe Under IE9 and IE10 Only Executes Once

After selecting a row, you can continuously insert it up or down, which essentially moves it!
After selecting a row, you can continuously insert it up or down, which essentially moves it!
After selecting a row, you can continuously insert it up or down, which essentially moves it!

The implementation code is as follows:

//Cancel the selection of the sorted row
$(‘#checkout’).live(‘click’,function(e){
if(!$(e.target).parent().hasClass(‘goods_row’)){
$(‘#checkout tr’).removeClass(‘select-tr’);
}
});
//Sorting, select the object to move
$(‘#checkout .goods_row’).live(‘click’,function(e){
if(!$(e.target).hasClass(‘goods_row’) && !$(e.target).hasClass(‘goods_number’)){
var self = $(this);
if(self.attr(‘merge_check’))
{//Bundle sale
$(‘#checkout tr’).not($(‘.’+self.attr(‘merge_check’))).removeClass(‘select-tr’);
$(‘.’+self.attr(‘merge_check’)).toggleClass(‘select-tr’);
}else
{//Single item sale
$(‘#checkout tr’).not(self).removeClass(‘select-tr’);
self.toggleClass(‘select-tr’);
}
}
});
//Swap order
$(document).keydown(function(e){
if($(‘.select-tr’).length>0 && !$(e.target).hasClass(‘goods_number’)){
var key = (e.keyCode) || (e.which) || (e.charCode);//Compatible with IE(e.keyCode) and Firefox(e.which)
if(key==38){//Up
var prev = $(‘.select-tr:first’).prev();
// If the previous row has the class name goods_row, it can be moved up; otherwise, it is an original product and cannot be moved up
if(prev.attr(‘merge_check’)){
$(‘.select-tr’).insertBefore($(‘.’+prev.attr(‘merge_check’)+’:first’));
}else{
$(‘.select-tr’).insertBefore(prev);
}
$(“.select-tr .change_goods_number”).focus();
$(“.select-tr .change_goods_number”).blur();
return false;
}else if(key==40){//Down
var next = $(‘.select-tr:last’).next();
if(next.attr(‘merge_check’)){
$(‘.select-tr’).insertAfter($(‘.’+next.attr(‘merge_check’)+’:last’));
}else{
$(‘.select-tr’).insertAfter(next);
}
$(“.select-tr .change_goods_number”).focus();
$(“.select-tr .change_goods_number”).blur();
return false;
}
}
});

Initially, I found that everything ran normally in Firefox, Chrome, and IE7/8. However, in IE9/10, it only ran correctly when opened in a new tab, and failed when embedded directly in an iframe. The reason seems to be that after executing insertAfter once, the focus is no longer inside the iframe, so it no longer responds to keydown (keyboard events). To solve this issue, I decided to call focus() on an element within the iframe. However, I discovered that .select-tr itself and its parent elements no longer support focus(). Using the previous sibling of .select-tr’s topmost parent element did work, but it caused the scrollbar to constantly jump to the top. The final solution I came up with is as follows:

$(“.select-tr .change_goods_number”).focus();
$(“.select-tr .change_goods_number”).blur();

I realized that although .select-tr itself and its parent elements no longer support focus(), the input element inside it still does!

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系