在我的HTML文件中,我使用图像来表示玩家可以点击的按钮。此按钮用作加载到游戏中的方式。当用户点击它时,我希望网页运行我的JavaScript文件。
目前,输入标记的处理方式如下:
<input type="image" src="Images/start.jpg" height='200' width='350' alt='Something went wrong loading the images. Update your Internet plugins and try again.'/>
我已经检查过很多网站,但是我看过的网站告诉我,onclick(我认为我可以使用的)只是一个按钮标签方面。
我错了吗?执行我的脚本最简单的方法是什么?
答案 0 :(得分:0)
onclick
也适用于type="image"
。你可以这样做:
HTML:
<input type="image" src="Images/start.jpg" height='200' width='350' alt='Something went wrong loading the images. Update your Internet plugins and try again.' onclick="yourScript();"/>
JS:
function yourScript()
{
//your JS here
}
答案 1 :(得分:0)
将单击处理程序添加到图像类型的输入元素中可以正常工作。
function INIT() {
alert('INITIALIZE GAME');
}
function UPDATE() {
alert('UPDATE GAME');
}
document.getElementById("startGame").addEventListener("click", function() {
INIT();
UPDATE();
});
<input type="image" src="https://i.stack.imgur.com/mRsBv.png?s=328&g=1" height='100' width='100' alt='Something went wrong loading the images. Update your Internet plugins and try again.' id="startGame" />
答案 2 :(得分:-1)
如果您想在用户点击图片时在Javascript页面中运行您的功能您必须使用此事件onClick:&#34; yourfunctionname()&#34;写下这个:
<input type="image" src="Images/start.jpg" height='200' width='350' alt='Something went wrong loading the images. Update your Internet plugins and try again.'onClick:"yourfunctionname()"/>