简体   繁体   English

如何在ap类中检查特定内容并在html文件中使用javascript创建if语句?

[英]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.

相关问题 如何编写jQuery / javascript if语句,以便在html时触发 <option>选择特定班级? - how do I write a jQuery/javascript if statement so that it's triggered when a html <option> with a specific class is selected? 如何使用JavaScript在特定InnerHTML上没有ID的类HTML元素上创建ID? - How do I create ID on a class HTML element on specific InnerHTML has no ID, with JavaScript? 如何根据我的 html 输入创建一个 javascript if 语句? - How do I create a javascript if statement depending on my html input? 我如何检查<p>元素为空使用类? - How do i check <p> element is empty using class? 我如何在此使用.remove <p> 有<a>特定类别的?</a> - How do I use .remove on this <p> that has an <a> with a specific class? Javascript/HTML DOM:如何检查文本用户输入是否等于 if 语句中的字符串 - Javascript/HTML DOM: How do I check if text user input equals to string in if statement 如何使用Javascript访问HTML p标签的内容 - How do I use Javascript to access the contents of an HTML p tag 如何在 JavaScript 的嵌入式 HTML 中创建 if 语句以显示 CSS 类 - How to create if statement inside embedded HTML in JavaScript to show CSS class 如何使用JavaScript在另一个if语句中创建if语句? - How do I create an if statement inside another if statement with javascript? 如何基于通过JavaScript添加的类更改元素的:: before内容? - How do I change the ::before content of an element based on a class that is added via JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM