为什么赢得<div>内部的文字会发生变化呢?

时间:2015-07-28 15:30:50

标签: javascript html

为什么赢得标签内的文字会发生变化?有人可以帮忙吗? 这是我的HTML和JS代码:

function pickStick(); {
    weapon = "Stick";
    weapondamage = 1;
    document.getElementById("weapon").innerHTML = "Weapon: ";

}

和...

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="script.js"></script>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<br><br>
<div id="weapon">You see a stick. You can <a href="" onclick="pickStick()">pick it up.</a></div>

</body>
</html>

3 个答案:

答案 0 :(得分:3)

您的函数名称(function pickStick();)后面有分号,需要删除它:

function pickStick() {
    weapon = "Stick";
    weapondamage = 1;
    document.getElementById("weapon").innerHTML = "Weapon: ";

}

然后,您可能希望从href中删除<a>,以便它不会移至新页面,或将其替换为href="#"

<a href="#" onclick="pickStick();">pick it up.</a>

答案 1 :(得分:0)

您需要更新2件事

JS更新

替换

function pickStick(); {

function pickStick() {

标记更新

替换

<a href="" onclick="pickStick()">

<a href="#" onclick="pickStick()"> 

<a href="javascript:void(0);" onclick="pickStick()">

供参考 - http://plnkr.co/edit/wu1y9OA1Ml1gQPCEQImB?p=preview

答案 2 :(得分:0)

This works:

function pickStick() {
    weapon = "Stick";
    weapondamage = 1;
    document.getElementById("weapon").innerHTML = "Weapon: ";
}
<div id="weapon">You see a stick. You can <a href="#" onclick="pickStick()">pick it up.</a>

So the problem is probably due to you having a semi-colon in your function definition, or the missing pound sign (#) in your anchor.