我有以下代码:
$(function () {
var target = $('span.slider') //can I make the variable apply to the target span?
target.mousedown(function() {
sliding = true
})
$(document).mouseup(function() {
sliding = false
})
$(document).mousemove(function(e) {
if(sliding){
target.css('left', e.pageX) //do I isolate here?
}
})
})
我的 html 中有四个“span.slider”。如何修改此 jquery 以便该功能仅适用于目标 span.slider?上面的代码移动了所有四个跨度,我完全理解为什么这样做。我无法将其定位到用户希望移动的范围。
最佳答案
试试这个:
$(function() {
var target = $('span.slider'); //can I make the variable apply to the target span?
var targetToMove;
target.mousedown(function() {
sliding = true;
targetToMove = $(this);
});
$(document).mouseup(function() {
sliding = false;
});
$(document).mousemove(function(e) {
if (sliding) {
targetToMove.css('left', e.pageX); //do I isolate here?
}
});
});
关于javascript - 当一个类存在多个项目时,Jquery 定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505885/