Javascript 如何在多个元素上使用onmousehave事件? 函数show_函数(){ var example_image=document.getElementById(“example_image”); example\u image.src=“example\u one.png”; } 函数隐藏_函数(){ var example_image=document.getElementById(“example_image”); example_image.src=“example.png”; }
当我将鼠标悬停在第一个div上时,图像会发生变化。但当我将鼠标悬停在第二个div上时,第一个div的图像也会发生变化Javascript 如何在多个元素上使用onmousehave事件? 函数show_函数(){ var example_image=document.getElementById(“example_image”); example\u image.src=“example\u one.png”; } 函数隐藏_函数(){ var example_image=document.getElementById(“example_image”); example_image.src=“example.png”; },javascript,html,css,Javascript,Html,Css,当我将鼠标悬停在第一个div上时,图像会发生变化。但当我将鼠标悬停在第二个div上时,第一个div的图像也会发生变化 有人只知道如何用javascript实现吗 正如丹科所说,ID必须是唯一的。下一步是在函数中插入变量: <html> <head> <script> function show_function() { var example_image=document.getE
有人只知道如何用javascript实现吗 正如丹科所说,ID必须是唯一的。下一步是在函数中插入变量:
<html>
<head>
<script>
function show_function() {
var example_image=document.getElementById("example_image");
example_image.src="example_one.png";
}
function hide_function() {
var example_image=document.getElementById("example_image");
example_image.src="example.png";
}
</script>
</head>
<body>
<div class="one" onmouseover="show_function()" onmouseout="hide_function()">
<img id="example_image" src="example.png">
</div>
<div class="two" onmouseover="show_function()" onmouseout="hide_function()">
<img id="example_image" src="example.png">
</div>
</body>
</html>
show_function = function (container) {
container.childNodes[1].src = "example_one.png";
}
hide_function = function (container) {
container.childNodes[1].src = "example.png";
}
函数显示\函数(id,隐藏)
{
var example_image=document.getElementById(id);
如果(隐藏){
example_image.src=“example.png”;
}否则{
example\u image.src=“example\u one.png”;
}
}
或者你也可以这样做:
<html>
<head>
<script>
function show_function(id, hide)
{
var example_image=document.getElementById(id);
if(hide){
example_image.src="example.png";
} else{
example_image.src="example_one.png";
}
}
</script>
</head>
<body>
<div class="one"
onmouseover="show_function('example_image1')"
onmouseout="show_function('example_image1', true)" />
<img id="example_image1" src="example.png">
</div>
<div class="one"
onmouseover="show_function('example_image2')"
onmouseout="show_function('example_image2', true)" />
<img id="example_image2" src="example.png">
</div>
</body>
</html>
希望有帮助 您可以这样做:
<img src="example.png"
onmouseover="this.src='example_one.png';"
onmouseout="this.src='example.png';" />
然后在HTML中,将此传递给函数:
<html>
<head>
<script>
function show_function() {
var example_image=document.getElementById("example_image");
example_image.src="example_one.png";
}
function hide_function() {
var example_image=document.getElementById("example_image");
example_image.src="example.png";
}
</script>
</head>
<body>
<div class="one" onmouseover="show_function()" onmouseout="hide_function()">
<img id="example_image" src="example.png">
</div>
<div class="two" onmouseover="show_function()" onmouseout="hide_function()">
<img id="example_image" src="example.png">
</div>
</body>
</html>
show_function = function (container) {
container.childNodes[1].src = "example_one.png";
}
hide_function = function (container) {
container.childNodes[1].src = "example.png";
}
我想您需要div的第一个子节点[0],并将悬停的对象传递到回调中
函数显示(e){
document.getElementById('label')。innerHTML=e.children[0]。id;
e、 style.border=“实心2px红色”;
}
函数隐藏(e){
e、 style.border=“0”;
}
将this
作为参数传递?将“this”作为函数参数传递,然后在函数中使用它,以所需的元素/相邻元素为目标。