Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

jquery - Javascript not working with HTML external link

I'm using Notepad++, and I know for sure that it works when linking different files to the main HTML file.Here is the HTML code I'm using:

    <link rel="stylesheet" type="style/css" href="adventure.css"></link>
    <script src="adventure.js"></script>
</head>
<body>
    <button onclick=log("testing")>Click Me</button>
    <div id="box">
        <div id="out"></div>
    </div>
</body>

And here is the JavaScript code:

function Gid(id) {
return getElementById(id);
}
function log(s) {
    Gid("out").innerHTML = s + "<br>" +
    Gid("out").innerHTML;
}

And the CSS for the divs

#box {
    width:500px;
    height:300px;
    border:5px solid black;
    overflow:auto;
}
#out {
    width:500px;
}

Please help me figure out why it's not working.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to call function on onclick event as :

onclick="log('testing')"

and you have to also use

return document.getElementById(id);

in Gid function

that's it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...