admin管理员组

文章数量:1287524

I have been looking at the following code for a few minutes and the problem is escaping me. How can I make each of these events that produce alerts work?

<html>

<head></head>

<body>

[ <span id="trad_alert">traditional alert</span> ]

[ <span onclick="inlineAlert ();">inline alert</span> ]

<script type="txt/javascript">
document.getElementById("trad_alert").onclick = function () {
    alert ("traditional alert");
}

function inlineAlert () {
    alert ("inline alert");
}
</script>

</body></html>

Searching Stack Overflow or the web produce numerous examples that appear identical to what I'm trying, but yet mine continues to fail.

I have been looking at the following code for a few minutes and the problem is escaping me. How can I make each of these events that produce alerts work?

<html>

<head></head>

<body>

[ <span id="trad_alert">traditional alert</span> ]

[ <span onclick="inlineAlert ();">inline alert</span> ]

<script type="txt/javascript">
document.getElementById("trad_alert").onclick = function () {
    alert ("traditional alert");
}

function inlineAlert () {
    alert ("inline alert");
}
</script>

</body></html>

Searching Stack Overflow or the web produce numerous examples that appear identical to what I'm trying, but yet mine continues to fail.

Share Improve this question edited Jan 10, 2023 at 9:29 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 30, 2011 at 4:40 soscsosc 1541 silver badge6 bronze badges 1
  • It looked ok to me too so I pushed it to jsbin (jsbin./afajo5/edit) and it ran just fine. At least in Chrome. – LeRoy Commented Apr 30, 2011 at 4:55
Add a ment  | 

3 Answers 3

Reset to default 4

Your script type is wrong.

Either change it to <script type="text/javascript"> or remove the type altogether.

If I change the mime type on the script block to text/javascript (you have txt) it works fine for me in Fx4.

You're missing an e in text/javascript. It works with one:

http://jsfiddle/kFUMX/

Have a look at Firebug for javascript debugging too.

本文标签: Simple JavaScript events alert using inline and traditional methodsStack Overflow