[英]How do I check for specific content in a p class and create an if statement based on that with javascript in an html file?
I'm trying to make a text box on my web page in which the content changes when you click around on the page, and to make it so that I can easily change the content of the text I'd like to use variables so I only have to change the content of the string variables instead of digging around in the html. 我正在尝试在我的网页上创建一个文本框,当你在页面上点击时内容会发生变化,并且这样我就可以轻松更改文本内容我想使用变量所以我只需改变字符串变量的内容而不是在html中挖掘。
So far I've managed to create the text box and make it possible to change from the first message to the second, but I haven't been able to figure out how to make an if statement to check for the presence of the second message and to display the third. 到目前为止,我已经设法创建了文本框,并且可以从第一条消息更改为第二条消息,但是我无法弄清楚如何创建if语句来检查第二条消息是否存在并显示第三个。
At the moment my code looks like this (with some stuff taken out, if you want to look at the full code the project is up on GitHub here ). 目前我的代码看起来像这样(有一些东西被取出,如果你想查看完整的代码,项目在这里是GitHub)。
Head: 头:
<head>
<script>
var text1 = "This is Emilia's portfolio site, where you can find her games, illustrations and interaction design projects, as well as a short bio and her contact info.";
var text2 = "What would you like to do?";
//set the first message the mascot character will say
window.onload = function starting () {document.getElementById("changetext").innerHTML = "안녕하세요! Emilia is in Korea right now, but if you need help I\'m here to guide you!";
}
//set what will happen once you click the textbox
function characterDialogue(){
//if (#changetext == "안녕하세요! Emilia is in Korea right now, but if you need help I\'m here to guide you!"){
document.getElementById('changetext').innerHTML = text1 ;
//}
}
</script>
</head>
Body: 身体:
<body onclick="characterDialogue()">
<div class="textbox">
<div class="textboxspeechtext">
<p id="changetext"></p>
</div>
</div>
</body>
Currently, the if statment present doesn't work at all. 目前,if语句存在根本不起作用。 I haven't really done any work with javascript before so I'm having trouble understanding how you link it together with html, and any similar problems that had been solved didn't seem to be directly applicable to what I want to do.
我之前没有真正完成任何javascript工作,所以我无法理解你如何将它与html链接在一起,任何已经解决的类似问题似乎都没有直接适用于我想要做的事情。 Thank you for reading.
谢谢你的阅读。
You need to get the innerHtml
back and compare against the variables:- 您需要返回
innerHtml
并与变量进行比较: -
var text1 = "안녕하세요! Emilia is in Korea right now, but if you need help I\\'m here to guide you!"; var text2 = "This is Emilia's portfolio site, where you can find her games, illustrations and interaction design projects, as well as a short bio and her contact info."; var text3 = "What would you like to do?"; //set the first message the mascot character will say window.onload = function starting() { document.getElementById("changetext").innerHTML = text1; } //set what will happen once you click the textbox function characterDialogue() { var current = document.getElementById('changetext').innerHTML; if (current == text1){ document.getElementById('changetext').innerHTML = text2; } if (current == text2){ document.getElementById('changetext').innerHTML = text3; } }
<body onclick="characterDialogue()"> <div class="textbox"> <div class="textboxspeechtext"> <p id="changetext"></p> </div> </div> </body>
Your IF statement compares "#changetext"
with the first text, this is incorrect. 您的IF语句将
"#changetext"
与第一个文本进行比较,这是不正确的。 Instead you need to get the current content of changetext
and compare it against text1
. 相反,您需要获取
changetext
的当前内容并将其与text1
进行比较。
Get the HTML content using: document.getElementById('changetext').innerHTML
使用:
document.getElementById('changetext').innerHTML
获取HTML内容
Set the HTML content using: document.getElementById('changetext').innerHTML = "New Text"
使用以下命令设置HTML内容:
document.getElementById('changetext').innerHTML = "New Text"
So your IF statement should look like: 因此,您的IF语句应如下所示:
if (document.getElementById('changetext').innerHTML == text1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.