即使它在 Chrome 浏览器上不起作用,也可以单击
Click even it not working on Chrome browser
嗨,我正在尝试在 JS 中制作拖放功能,它在 Firefox 中工作正常,但在 chrome 上不起作用。我认为这与Event Deligation
有关,我正在附加我的代码库的链接。以下是重现该问题的步骤:
- 创建新任务
- 将其拖到另一列
- 现在单击编辑或删除图标(圆圈中的E和D)。
以下是代码亮点(代码有点大,你可以在Codepen上检查它):
.JS:
$(function(){
function init(){
var mouseX = 0, // Mouse Position
mouseY = 0,
elmX = 0, // Element Position
elmY = 0,
pillers = $('.pillers'), // Task Container
pillerWidth = $('.pillers:nth-child(1)').width(), // Taks Container width
currentElm; // Current Element
/* When Left Mouse Button is Pressed */
$('.dragable').on('mousedown', function(e){
var temp;
$(this).addClass('rel');
mouseX = e.clientX; // Current Mouse Position and Store it to global variables
mouseY = e.clientY;
temp = +($(this).css('left').slice(0, -2)); // Get Element Position and if it not a number then change it to 0
elmX = null || isNaN(temp) ? 0 : temp;
temp = +($(this).css('top').slice(0, -2));
elmY = null || isNaN(temp) ? 0 : temp;
$(this).css({'z-index':'9999'}); // Increase the Z-Index of the Element so that it wont be overlapped by other element.
currentElm = $(this); // set the current value so that it could be use by mouse move
/* Some Hack for not let heighlight the data(Copied from net) */
document.body.focus();
document.onselectstart = function () { return false; };
$(this).ondragstart = function() { return false; };
return false;
}).on('mouseup',function(e){ // This will be fired when Mouse Button release back
if(currentElm !== null){
currentElm.removeClass('rel').prependTo('.arrived .tasks').css({ // Resetting the Position Object
left: 0,
top: 0
});
currentElm.css({'z-index' : '1'}); // Set Z-Index back to normal value.
currentElm = null; // Finally Set the Current Element to null so that it won't get dragged any more
}
}).on("mousemove", function(e){ // Mouse Move Event .. This is the main part, It will reposition the element with mouse pointer
if(currentElm !== undefined && currentElm !== null){
currentElm.addClass('draged').css({ // This sets the position of div element
left : (elmX + e.clientX - mouseX)+'px',
top : (elmY + e.clientY - mouseY)+'px'
});
/* Set Appropriate Class to Piller to Which The Element is going to be added */
if( e.clientX >= $('.pillers:nth-child(1)').offset().left && e.clientX < ($('.pillers:nth-child(1)').offset().left+pillerWidth) && e.clientY < $('.pillers:nth-child(1)').outerHeight()){
$('.pillers:nth-child(1)').addClass('arrived').siblings('.pillers').removeClass('arrived');
}else if(e.clientX >= $('.pillers:nth-child(2)').offset().left && e.clientX < ($('.pillers:nth-child(2)').offset().left+pillerWidth) && e.clientY < $('.pillers:nth-child(2)').outerHeight()){
$('.pillers:nth-child(2)').addClass('arrived').siblings('.pillers').removeClass('arrived');
}else if(e.clientX >= $('.pillers:nth-child(3)').offset().left && e.clientX < ($('.pillers:nth-child(3)').offset().left+pillerWidth) && e.clientY < $('.pillers:nth-child(3)').outerHeight()){
$('.pillers:nth-child(3)').addClass('arrived').siblings('.pillers').removeClass('arrived');
}else if(e.clientX >= $('.pillers:nth-child(4)').offset().left && e.clientX < ($('.pillers:nth-child(4)').offset().left+pillerWidth) && e.clientY < $('.pillers:nth-child(4)').outerHeight()){
$('.pillers:nth-child(4)').addClass('arrived').siblings('.pillers').removeClass('arrived');
}
}
});
$('a.remove').on('click',function(){
console.log('hey')
$(this).parents('.dragable').remove();
});
$('.add_task_button').on('click',function () {
var place= $(this).closest('.create_task_box'),
titl=place.find('input#title').val(),
disc=place.find('textarea#discription').val(),
time = new Date(),
format = time.toLocaleDateString();
if(titl || disc){
var val = $('.temp').clone(true).removeClass('temp hide').insertBefore(place);
val.find('#TaskHeading').val(titl).end().find('#task-discription').text(disc).end().find('.time').text(format).css({
left: 0,
top: 0
});
}
$('input#title, textarea#discription').val('');
});
$(document).on("click", ".edit", function(){
e.stopPropagation();
if($(this).is('.done')){
$(this).removeClass('done');
$(this).closest('.task-unit').addClass('dragable').find('input, textarea').attr('readonly', 'readonly').addClass('readonly');
}else{
$(this).addClass('done');
var task = $(this).closest('.dragable');
task.removeClass('dragable').find('input, textarea').removeAttr('readonly').removeClass('readonly');
}
});
}
init();
});
我在这里不提到HTML和CSS部分,因为它会占用很多空间。你可以在Codepen上看到完整的代码。
如果还有其他需要,请告诉我。
问题是,你有一个对象(父对象)有一个mousedown
事件,在它里面,另一个对象(子对象)有一个click
事件。似乎在Chrome中,第一个事件(鼠标向下)是捕获按钮上的单击。
作为解决方法,您可以执行以下操作:
- 具有父元素上的
mousedown
事件函数。 - 当用户对子级执行
mouseover
事件时,取消绑定mousedown
事件。 - 当用户对父函数执行
mouseout
时,再次将函数绑定到父函数。
举个例子:
$(".theparent").on("mousedown",function(){
doThings();
});
$(".thechildren").on("click",function(){
alert("Child");
});
$(".thechildren").on("mouseover",function(){
$(this).closest(".theparent").off("mousedown");
console.log("Off");
});
$(".thechildren").on("mouseout",function(){
$(this).closest(".theparent").on("mousedown",doThings);
console.log("on");
});
function doThings(){
alert("Parent");
}
.theparent{
width:200px;
height:100px;
background-color:#3a3a3a;
position:absolute;
}
.thechildren{
position:relative;
background-color:#FF0000;
width:50px;
height:50px;
cursor:pointer;
left:50px;
top:25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="theparent">
<div class="thechildren">Child</div>
</div>
看到它在小提琴上工作。
相关文章:
- Javascript的某些部分在Chrome中不起作用,但在其他浏览器中可以完美工作
- .hasClass 在 onclick 函数中不起作用,但删除和添加类函数可以
- Backbone.js 不起作用,而 Underscore、JSON 和 jQuery 可以
- 鼠标点击不会'不起作用,鼠标移动可以
- Javascript不起作用,但当放置断点并从chrome调试器工具中单击文件时,它可以正常工作
- CefSharp javascript元素点击不;不起作用,但手动点击它可以
- 如果我在其中使用alert,Javascript代码就可以工作,否则它'It’不起作用
- 当我尝试在jsFiddle中暂停并播放时,我的代码可以工作,但它不起作用;我的html页面不起作用
- DomElement.click() 事件在 chrome 中不起作用,但其他事件可以工作
- 编程新手,可以'我不明白为什么这个因子计算器没有;不起作用
- 即使它在 Chrome 浏览器上不起作用,也可以单击
- Angularjs 双向数据绑定不起作用;$watch也不起作用
- 即使我从另一个工作小提琴复制代码,JSFiddle 也不起作用
- 跨域 AJAX 不起作用,即使使用通配符来源也是如此
- rails 预编译 为什么 javascript 在本地不起作用,但在 heroku 它可以工作
- jQuery中的标签功能即使在添加jQuery库后也不起作用
- href 在导航栏中不起作用.有人可以告诉我如何让它工作
- 滑动条触发器不起作用(也称为滑动条)
- 我的javascript函数没有'似乎不起作用.我可以'我不知道为什么
- Javascript进度条在Chrome中不起作用,在firefox中也可以