JS 函数不更新 HTML 字符串
JS function does not update HTML string
我对JS和HTML很陌生(~20小时前开始),并且已经有一个问题:下面你可以看到我的代码。正如一个教程所说,单击按钮将更改状态线文本。但是出了点问题,我无法弄清楚。
<!DOCTYPE html>
<html>
<head>
<title>Некое подземелье</title>
</head>
<body>
<p id="statusLine">Вы попали в подземелье.</p>
<button type="button" onclick="goDeeper()">Идти глубже в подземелье</button>
<script>
function goDeeper()
{
var nextEvent=(Math.floor(Math.random()*10+1));
switch(nextEvent){
case'1':
document.getElementById("statusLine").innerHTML="Вам на пути попался гоблин!";
break;
}
}
</script>
</body>
</html>
所以,有些不对劲。我应该怎么做才能解决这个问题?
尝试使 case 语句与数字1
匹配,而不是字符串'1'
:
function goDeeper()
{
var nextEvent = Math.floor(Math.random()*10+1);
switch(nextEvent) {
case 1:
document.getElementById("statusLine").innerHTML="Вам на пути попался гоблин!";
break;
}
}
或者就此而言,如果只需要匹配一个条件,只需摆脱switch
并使用简单的if
块:
function goDeeper()
{
var nextEvent = Math.floor(Math.random()*10+1);
if (nextEvent == 1) {
document.getElementById("statusLine").innerHTML="Вам на пути попался гоблин!";
}
}
What I understand you want to change the text on click button so Try this
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="Hello World";
}
</script>
</head>
<body>
<p>Click the button to trigger a function.</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>`enter code here`
</body>
</html>
Refrence Link : http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick
相关文章:
- 将html元素插入到文本字符串中,以匹配另一个html字符串
- HTML字符串作为上下文
- 正在分析HTML字符串并从中进行选择
- 将GET请求(HTML字符串)转换为完整的DOM对象
- 在树枝模板上从数据库中解压缩HTML字符串
- 如何使用Razor显示模板将字符串转换为html字符串
- 如何在不打断标记的情况下突出显示html字符串中的文本
- 使用val()在jquery循环中设置html字符串的val
- 搜索并用jQuery替换整个HTML字符串
- 替换 html 字符串中的某些“<”和“>”
- Javascript-将包含变量的html字符串作为参数传递
- 如何用JavaScript替换HTML字符串
- 如何在我的模板(Angular2/TypeScript)中将HTML字符串转换为HTML
- 在HTML字符串的某个段落后插入HTML
- 使用 javascript 压缩包含嵌入图像的 HTML 字符串
- 需要使用Java编码/转义HTML字符串,并在JavaScript中解码HTML字符串
- 在不更改 HTML 的情况下操作 HTML 字符串的内容
- 使用JavaScript从HTML字符串中提取文本
- regex在JavaScript中拆分一个html字符串
- 创建HTML对象的Javascript与创建HTML字符串的比较