QQ726535609 QQ726535609 - 1 year ago 48
HTML Question

Why doesn't this onclick handler on an img work?

I use a

, set
method, but it cannot call the method:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<div style="width: 300px; height: 200px;" id="img-div" >
<img id="img-id" src="resources/01.jpg" width="200px" height="80px" style="CURSOR:pointer;" onclick="close();">
<script type="text/javascript">
function close(){




Why when I click the
, it can not execute the
I really don't know how to resolve the issue.

Answer Source

When you define a function named close on the global context (window, in case of the browser), you're actually defining it as window.close. But, since window.close is reserved for the purpose of closing the current window, they collide.

Change the name to something less generic:

function closeIt() {
    // ...