gpt4 book ai didi

javascript - mousemove 不适用于对象元素

转载 作者:行者123 更新时间:2023-11-27 23:22:18 25 4
gpt4 key购买 nike

有没有办法让 mousemove 事件处理程序在 HTML 标记上工作?我有这样的 HTML:

<object type="image/svg+xml" data="myfile.svg"></object>
<img src="myfile.svg"/>

和一些像这样的 JavaScript/jQuery:

$("img, object").on("mousemove", function() {
$("body").css("background-color","#f0f");
});
$("img, object").on("mouseleave", function() {
$("body").css("background-color","transparent");
})

但是 mousemove 只在 img 标签上起作用。将 pointer-events: all 应用于 objectobject * 似乎没有帮助。

这是一个fiddle .

$("img, object").on("mousemove", function() {
$("body").css("background-color","#f0f");
});

$("img, object").on("mouseleave", function() {
$("body").css("background-color","transparent");
})
img, object {
width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Mouse over the images to change the background color. It doesn't seem to work for the object tag.

<h2>SVG as &lt;object&gt;</h2>

<object type="image/svg+xml" data="https://upload.wikimedia.org/wikipedia/commons/e/ea/Converted_to_SVG.svg"></object>

<h2>SVG as &lt;img&gt;</h2>

<img src="https://upload.wikimedia.org/wikipedia/commons/e/ea/Converted_to_SVG.svg"/>

最佳答案

您是否尝试过使用 onmouseenter?还是您希望在鼠标移动时执行特定的操作?

如果是这样,我建议你将对象包装到一个内联div中,并将监听器添加到div中,并将对象上的指针事件设置为无。

请看下面的例子:

$("img, .objectwrap").on("mousemove", function() {
$("body").css("background-color","#f0f");
});

$("img, .objectwrap").on("mouseleave", function() {
$("body").css("background-color","transparent");
})
img, object {
width: 100px;
}
object {
pointer-events:none;
}
.objectwrap {
display:inline-block;
background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Mouse over the images to change the background color. It doesn't seem to work for the object tag.

<h2>SVG as &lt;object&gt;</h2>

<div class="objectwrap">
<object type="image/svg+xml" data="https://upload.wikimedia.org/wikipedia/commons/e/ea/Converted_to_SVG.svg"></object>
</div>
<h2>SVG as &lt;img&gt;</h2>

<img src="https://upload.wikimedia.org/wikipedia/commons/e/ea/Converted_to_SVG.svg"/>

关于javascript - mousemove 不适用于对象元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40511362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com