javascript - 来自字符串函数的图像

标签 javascript arrays string

我正在尝试制作一个神奇的八球模拟,但它对我不起作用。 Chrome 中的检查器元素没有显示任何错误,所以我很困惑为什么它不起作用。任何建议将不胜感激。

<!DOCTYPE html>
<html lang="en">
<head>

    <title>Project 4: Consistent</title>
    <!-- This part is the function to create my magic eight ball that will randomly give a result, but for certain questions,
    it will provide the same answer always to fool their minds. -->

    <script>

        var answerMap = {}

        var images = ['eightBallYes.png', 'eightBallNo.png', 'eightBallMillionYears.png', 'eightBallAskLater.png', 'eightBallReally.png'];

        //I actually had a little bit of difficulty with this part of the project. 
         //The answer.search method you showed us in class for some reason is not working for me.
        //I worked with the GTF on this part 

        function eightBall() {
            var answer = document.getElementById("answerBox").value;
            answer = answer.toLowerCase();

            if (answer.search(/[r]/) > 0 ) {
                var yes = '../Images/eightBallYes.png' 
                return yes;
            }

            if (answer.search(/[m]/) > 0 ) {
                var no = '../Images/eightBallNo.png' 
                return no;
            }

        }

        window.onload = alert('Welcome to my Project 4')

    </script>
</head>

<body>
<body style="background:#EEEE17">
    <!-- This part of the page will simulate a magic eight ball that will provide at least 4 answers.
    When certain questions are asked, it will return the same answers. This is honestly a pretty cool project to work on. -->

    <div style="text-align:center">
        <h1>Project 4: Booyah!</h1>
        <img src="../images/eightBallTemplate.png" >
        <h2>Magic 8-Ball Game</h2>


        <input type="text" id="answerBox" value="Please ask a question">
        <input type="button" value="Submit for Magical Results" onclick='eightBall()'/>


        <div id="myOutput"></div>

        <hr>

        <a href="http://pages.uoregon.edu/alans/111/CIS%20111/p4/mac.html">Old MacDonald In-Class Activity</a>
        <br>
        <a href="http://pages.uoregon.edu/alans/111/CIS%20111/p4/paramString.html">Parameter In-Class Activity</a>
        <br>
        <a href="http://pages.uoregon.edu/alans/111/CIS%20111/p4/isPrimeLight-jQuery.html">jQuery In-Class Activity</a>
        <br>
        <a href="http://pages.uoregon.edu/alans/111/CIS%20111/p4/string.html">String In-Class Activity</a>

        <footer>

            <p>
                &copy; Copyright  by Alan Sylvestre
            </p>
        </footer>
    </div>
</body>

最佳答案

您没有对函数的返回值执行任何操作。

您可能想将其分配给 <img>标签的src属性。

关于javascript - 来自字符串函数的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15194449/

相关文章:

javascript - 如何改变 react 中三个组件之一的状态

javascript - 下划线: How to return all values by array with keys

Javascript 多维数组为空

python - 数组解释的循环旋转

c - 字符串排列 - 这种回溯递归是如何工作的?

javascript - 为什么在 Facebook Flux 上使用 Redux?

javascript - 通过迭代数组将方法分配给对象

java - 以编程方式区分最终参数数组和最终参数省略号的任何方法?

c++ - 当 malloc()-ing 相同的结构时,如何在结构中使用 C++ 字符串?

c++ - 如何使用标准库函数通过错误检查将字符串 (char*) 转换为数字?