![](/img/trans.png)
[英]how do I write a jQuery/javascript if statement so that it's triggered when a html <option> with a specific class is selected?
[英]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?
我正在尝试在我的网页上创建一个文本框,当你在页面上点击时内容会发生变化,并且这样我就可以轻松更改文本内容我想使用变量所以我只需改变字符串变量的内容而不是在html中挖掘。
到目前为止,我已经设法创建了文本框,并且可以从第一条消息更改为第二条消息,但是我无法弄清楚如何创建if语句来检查第二条消息是否存在并显示第三个。
目前我的代码看起来像这样(有一些东西被取出,如果你想查看完整的代码,项目在这里是GitHub)。
头:
<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 onclick="characterDialogue()">
<div class="textbox">
<div class="textboxspeechtext">
<p id="changetext"></p>
</div>
</div>
</body>
目前,if语句存在根本不起作用。 我之前没有真正完成任何javascript工作,所以我无法理解你如何将它与html链接在一起,任何已经解决的类似问题似乎都没有直接适用于我想要做的事情。 谢谢你的阅读。
您需要返回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>
您的IF语句将"#changetext"
与第一个文本进行比较,这是不正确的。 相反,您需要获取changetext
的当前内容并将其与text1
进行比较。
使用: document.getElementById('changetext').innerHTML
获取HTML内容
使用以下命令设置HTML内容: document.getElementById('changetext').innerHTML = "New Text"
因此,您的IF语句应如下所示:
if (document.getElementById('changetext').innerHTML == text1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.