简体   繁体   English

为什么里面的文字没有 <div> 变化,什么时候应该明确?

[英]Why won't the text inside <div> change, when it clearly should?

Why won't the text inside the tags change, when it clearly should change? 为什么标记中的文本显然应该更改,但标记中的文本为什么不更改? Can anyone help? 有人可以帮忙吗? Here is my HTML and JS code: 这是我的HTML和JS代码:

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

}

And... 和...

<!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>

You have a semicolon after your function name ( function pickStick(); ), you need to remove that: 函数名称后面有分号( function pickStick(); ),您需要删除该分号:

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

}

Then you may want to remove the href from your <a> so it doesn't move to a new page, or replace it with href="#" : 然后,您可能希望从<a>删除href ,以使其不会移动到新页面,或将其替换为href="#"

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

You need to update 2 things 您需要更新2件事

JS update JS更新

Replace 更换

function pickStick(); {

with

function pickStick() {

Markup update 标记更新

Replace 更换

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

with

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

or 要么

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

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

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. 因此,问题可能是由于您在函数定义中使用了分号,或者在锚中缺少了井号( # )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM