문제

<body onload="alert("Hello World")">
<h1>Hello World!</h1>
</body>

why doesn't this work?

EDIT Additionally, why does chrome dev tools report "Uncaught SyntaxError: Unexpected token } " that as the error message?

Thanks, funbeans

도움이 되었습니까?

해결책 2

Try this

<body onload="alert('Hello World')">
<h1>Hello World!</h1>
</body>

The issue is with nested quotes

Here is a working demo

다른 팁

The issue is with double quotes, you need to escape them:

<body onload="alert(\"Hello World\")">

Or use single quotes:

<body onload="alert('Hello World')">

This happens because when you use double quote HTML think that your attribute is ended and contains of only alert( and the rest "Hello World")" is another attribute.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top