Javascript 显示未捕获的TypeError:e.target.setCapture不是chrome中的函数
我在chrome中在body onload上调用init()时遇到这个错误,但在mozilla firefox中工作正常Javascript 显示未捕获的TypeError:e.target.setCapture不是chrome中的函数,javascript,html,google-chrome,Javascript,Html,Google Chrome,我在chrome中在body onload上调用init()时遇到这个错误,但在mozilla firefox中工作正常 未捕获类型错误:e.target.setCapture不是一个函数 如果chrome不支持setCapture(),我们该怎么办 function init() { var btn = document.getElementById("anchorId"); btn.addEventListener("mousedown", mous
未捕获类型错误:e.target.setCapture不是一个函数
如果chrome不支持setCapture(),我们该怎么办
function init() {
var btn = document.getElementById("anchorId");
btn.addEventListener("mousedown", mouseDown, false);
btn.addEventListener("mouseup", mouseUp, false);
}
function mouseDown(e) {
e.target.setCapture();
e.target.addEventListener("mousemove", mouseMoved, false);
}
function mouseUp(e) {
e.target.removeEventListener("mousemove", mouseMoved, false);
}
function mouseMoved(e) {
var x = e.clientX;
var y = e.clientY;
var anchorPathWidth = document.getElementById('anchorPath').offsetWidth;
document.getElementById('anchorId').style.position = "absolute";
document.getElementById('anchorId').style.left = (x * 100)/anchorPathWidth + "%";
}
Chrome不支持此功能请参见检查此更新:
function init() {
alert(1);
var btn = document.getElementById("anchorId");
btn.addEventListener("mousedown", mouseDown, true);
btn.addEventListener("mouseup", mouseUp, true);
}
function mouseDown(e) {
console.log("Mouse Down");
console.log(e.target)
if(e.target.addEventListener)
{
e.target.addEventListener("mousemove", mouseMoved, true);
}
else
{
if(e.target.setCapture)
{
e.target.setCapture();
}
}
}
function mouseUp(e) {
e.target.removeEventListener("mousemove", mouseMoved, true);
console.log("Mouse Up");
}
function mouseMoved(e) {
var x = e.clientX; // Get the horizontal coordinate
var y = e.clientY; // Get the vertical coordinate
var coor = "X coords: " + x + ", Y coords: " + y;
console.log("Move the Div to " + coor);
var anchorPathWidth = document.getElementById('anchorPath').offsetWidth;
document.getElementById('anchorId').style.position = "absolute";
document.getElementById('anchorId').style.left = (x * 100)/anchorPathWidth + "%";
//console.log( x/anchorPathWidth + "%");
}
实时演示:无需在chrome浏览器中调用setCapture()方法,仅适用于mozilla浏览器
使用e.target.addEventListener(“mousemove”,mouseMoved,true);含铬
function mouseMoved(e) {
var output = document.getElementById("output");
console.log("Position: " + e.clientX + ", " + e.clientY);
}
@Anoop B.K,他的解决方案会很好地工作,因为他正在mouseDown事件方法中检查条件“e.target.addEventListener”或e.target.setCapture是否为真。在chrome中有其他方法吗?我肯定有,但我有点困惑。setCapture实际用于什么?请参阅此[链接]()好的,我正在寻找setCapture()的替代方案对于chromeNow,它可以工作,但在鼠标向下移动后,如果你将其向左拖动,则捕获无法正常工作。是的,我甚至注意到..当它缓慢完成时,它可以工作。当你向左拖动它时,会发生一些链接拖动或图像拖动…因为mousup事件不起作用。只有在不添加jquerySo的情况下,才会发生链接拖动和图像拖动,最好在里面添加jquery,请通过此链接