Javascript 如何点击第二张图片
我的网页加载了两个东西:一个图像和一个画布 页面加载时,网页将加载图片。 如果单击某个位置,页面将加载画布 现在我想,如果我点击图片,会发生一些事情,但是当点击图片时,如何说onclick-> 我想我可以用我照片上的身份证工作。但是在创建图片时,如何在javascript中提供iDJavascript 如何点击第二张图片,javascript,Javascript,我的网页加载了两个东西:一个图像和一个画布 页面加载时,网页将加载图片。 如果单击某个位置,页面将加载画布 现在我想,如果我点击图片,会发生一些事情,但是当点击图片时,如何说onclick-> 我想我可以用我照片上的身份证工作。但是在创建图片时,如何在javascript中提供iD <script type="text/javascript"> function start() { var vierkant = document.getElementById(
<script type="text/javascript">
function start() {
var vierkant = document.getElementById("Vierkant");
initWebGL(vierkant); // Initialize the GL context
// Only continue if WebGL is available and working
if (gl) {
gl.clearColor(0.0, 0.0, 5.0, 1.0); // Set clear color to black, fully opaque
gl.enable(gl.DEPTH_TEST); // Enable depth testing
gl.depthFunc(gl.LEQUAL); // Near things obscure far things
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT); // Clear the color as well as the depth buffer.
}
}
function startImage()
{
var img = document.createElement("img");
img.src = "Bus_Stop_2.png";
document.body.appendChild( img );
img.
}
function initWebGL(vierkant) {
// Initialize the global variable gl to null.
gl = null;
try {
// Try to grab the standard context. If it fails, fallback to experimental.
gl = vierkant.getContext("webgl") || vierkant.getContext("experimental-webgl");
}
catch(e) {}
// If we don't have a GL context, give up now
if (!gl) {
alert("Unable to initialize your 'Vierkant'.");
}
}
</script>
<body onclick="start()" onload="startImage()">
<canvas id="Vierkant" width="640" height="480">
Your browser doesn't appear to support the HTML5 <code><canvas></code> element.
</canvas>
</body>
只需在创建图像后执行此操作
img.onclick = function() { alert('image clicked') };
你可以用你的工作代替警戒 这应该有效:
function startImage()
{
var img = document.createElement("img");
img.src = "Bus_Stop_2.png";
document.body.appendChild( img );
img.onclick = function (evt) {
// do something here
};
}
要设置id,可以使用:
img.setAttribute('id','myid')代码>Javascript中的事件处理程序区分大小写,因此您的代码恐怕无法工作;应该是img.onclick
是的,我注意到了!谢谢