鼠标监听器不能处理innerHTML和图库中的图像
Mouse listeners not working with innerHTML and images in a gallery
基本上,对于这段代码,我希望在单击时显示图片,如我的onmousedown
函数所示。但是由于某些原因,当我点击图片时,它没有显示我在server()
函数中调用的图片。除此之外,我的计数器(按钮)不工作,因为他们应该(加减)。
<!-- HTML header and stylesheets -->
memory = 0;
hdd = 0;
usb = 0;
server = "";
function allInOne()
{
document.getElementById("memory").innerHTML = memory;
document.getElementById("hdd").innerHTML = hdd;
document.getElementById("usb").innerHTML = usb;
}
function server();
{
div = document.getElementById("server");
div.innerHTML = "<img src = 'server1.png' />";
}
<!-- omitted some HTML -->
<span> Memory (GB) <span>
<button onmousedown="allInOne();memory++">+</button>
<article id = "memory">0</article>
<button onmousedown="allInOne();memory--">-</button>
<br />
<span> HDD (GB) </span>
<button onmousedown="allInOne();hdd++">+</button>
<article id = "hdd">0</article>
<button onmousedown="allInOne();hdd--">-</button>
<br />
<span> USB Ports </span>
<button onmousedown="allInOne();usb++">+</button>
<article id = "usb">0</article>
<button onmousedown="allInOne();usb--">-</button>
<br />
<span class = "button" onmousedown="mac"> Mac OS X </span>
<span class = "button" onmousedown="linux"> Linux </span>
<span class = "button" onmousedown="windows"> Windows </span>
<br />
<img src = "server1.png" onmouseover="server();" />
<img src = "laptop.jpg" />
<img src = "0009-03_lenovo_pc.jpg"/>
<!-- some more HTML below -->
如果有人能帮忙,我将不胜感激。
好了。
<script type = "text/javascript">
memory = 0;
hdd = 0;
usb = 0;
server = "";
function allInOne()
{
document.getElementById("memory").innerHTML = memory;
document.getElementById("hdd").innerHTML = hdd;
document.getElementById("usb").innerHTML = usb;
}
function server1()
{
var div_server = document.getElementById("server");
div_server.innerHTML = "<img src = 'i_icon.gif' />";
}
</script>
- 删除a;
server()
- 将方法重命名为
server1()
,因为变量被命名为server。
将关闭标签<span>
改为</span>
在调用方法allInOne()之前移动了所有的++和——操作。
将所有事件更改为onclick。我使用了与系统不同的映像。
,它工作良好。请现在测试
从javascript中添加事件侦听器会更好更有效。每个onmousedown
、onclick
等触发一个eval
动作。有两种方法可以将事件监听器添加到HTML元素:[element].on[action] = [some function]
或使用HTML元素的addEventListener
/attachEvent
方法。
看一下这个jsfiddle的例子,看看你是否可以把它应用到你的代码中
试试下面的
<!DOCTYPE html>
<html>
<head>
<title> Javascript </title>
<link rel = "stylesheet" type = "text/css" href = "css.css">
<script type = "text/javascript">
memory = 0;
hdd = 0;
usb = 0;
server = "";
function allInOne()
{
document.getElementById("memory").innerHTML = memory;
document.getElementById("hdd").innerHTML = hdd;
document.getElementById("usb").innerHTML = usb;
}
function server()
{
div = document.getElementById("server");
div.innerHTML = "<img src = 'server1.png' />";
}
function changeText(id)
{
if(id == 'mac')
{
txt = document.getElementById("mac");
txt.innerHTML = "mac";
}
else if(id =='linux' )
{
txt = document.getElementById("linux");
txt.innerHTML = "linux";
}
else
{
txt = document.getElementById("windows");
txt.innerHTML = "windows";
}
}
</script>
</head>
<body>
<div>
<span> Memory (GB) <span>
<button onmousedown="allInOne();memory++">+</button>
<article id = "memory">0</article>
<button onmousedown="allInOne();memory--">-</button>
<br />
<span> HDD (GB) </span>
<button onmousedown="allInOne();hdd++">+</button>
<article id = "hdd">0</article>
<button onmousedown="allInOne();hdd--">-</button>
<br />
<span> USB Ports </span>
<button onmousedown="allInOne();usb++">+</button>
<article id = "usb">0</article>
<button onmousedown="allInOne();usb--">-</button>
<br />
<span class = "button" onmousedown="changeText(this.id);" id="mac"> Mac OS X </span>
<span class = "button" onmousedown="changeText(this.id);" id="linux"> Linux </span>
<span class = "button" onmousedown="changeText(this.id);" id="windows"> Windows </span>
<br />
<img src = "server1.png" onmouseover="server();" />
<img src = "laptop.jpg" />
<img src = "0009-03_lenovo_pc.jpg"/>
<br />
<div id = "server"></div>
</div>
</body>
</html>
You did one mistake while declaring the function with ; like the following
function server();
{
}
您需要删除当前位于行尾的分号:
function server();
关于按钮:你实际上并没有说你的添加/减去按钮行为有什么问题,但我猜问题是你首先通过allInOne()
函数更新显示,然后在此之后增加或减少。
与其说:
onmousedown="allInOne();memory++"
试题:
onmousedown="memory++;allInOne();"
// or, unless you have a specific reason for using mousedown rather than click
onclick="memory++;allInOne();"
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 角度:在ng重复上切换图像
- 画廊图像未显示
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- Javascript排序的图像弹出窗口..可以't单独弹出
- 画布数据到图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使图像在单击时展开到不大于浏览器
- Html页面上的多个Base64图像和平滑加载
- UIAutomation放大图像
- 使用javascript在Flash中加载外部图像
- 可以在这里为背景图像设置滤镜吗
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 使用 js 将背景图像设置为 HTML
- Javascript将图像src更改为淡入淡出
- Javascript全局onclick监听器
- 如何将事件监听器添加到画布中的图像
- 鼠标监听器不能处理innerHTML和图库中的图像