2016-11-20 64 views
1

我有一个函数将文本附加到带有某种颜色效果的div。 我想让文字出现在单词之后。 (但是我只需要在第二个函数的参数“str”的文本上使用这个效果,那么“who”必须显得正常。 我找到了一个我需要的效果代码,但是我不知道如何使两个功能一起工作,因为我想如何使所需的文本出现在单词之后

JS:

// my function 
    var showText = function(who, str) { 
     if(str !== "") { 
     $("#storyBoard").append("<p><span style='color:#323232;'>" + who.name + ": " + "</span>" + str + "</p>"); 
     }; 
    }; 

showText(somObj, "The text i want to append on the page when calling the function"); 

    // the function i want to add into my first function 
    $(function() { 
     var a = new String; 
     a = $('.text_cont_inner').text(); 
     $('.text_cont_inner').text(''); 
     var c = a.length; 
     j = 0; 
     setInterval(function() { 
     if(j < c) { 
      $('.text_cont_inner').text($('.text_cont_inner').text() + a[j]); 
      j = j + 1; 
     } else { 
      $('.text_cont_inner').removeClass('after') 
     } 
     }, 100); 
    }); 

HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <link rel="stylesheet" type="text/css" href="css/style.css"> 
    <title>MacroG0D - My first game</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script src ='Js/main.js' type="text/javascript"> </script> 
</head> 
<body> 
<div id = "storyBoard"> 
<h1> Star Wars - Healing <br>The Breach:<hr></h1> 

<p class="intro">A long time ago, in a galaxy far, far away</p> 
<p> Welcome to the virtual pseudo-reality. Are you ready to start this journey? </p> 
</div> 
<input type="text" id ="myInput" placeholder="Type the command here" name="myInput" autofocus autocomplete='off'/> 
<button id="btn" type="submit"> Enter </button> 

</body> 
</html> 
+0

显示您的html代码 – Mahi

+1

片段仅用于可运行代码。如果您的代码仅包含JavaScript,请避免使用它们。 – 2016-11-20 19:53:59

+0

我加了我的html代码,还有'showText'函数调用来显示我的问题。 – MacroG0D

回答

1

试试这个:)

var showText = function(who, str) { 
    if(!str){ 
    return; 
    } 

    var textNode = $("#storyBoard") // take container 
    .append("<p><span style='color:#323232;'>" + who.name + ": </span><span></span></p>") // add contents 
    .find("p span:last-сhild")[0]; // select last <span> to put str value into 

    var words = str.split(" "); // break a string on words 
    var intervalId = setInterval(function() { // set new interval and remember it's ID 
    if (!words.length) { // no words left in array 
     return clearInterval(intervalId); // stop running interval and exit 
    } 

    textNode.innerText += " " + words.shift(); // add space and next word 
    }, 100); // milliseconds between word insertions 
}; 
+0

得到此错误:语法错误,无法识别的表达式:不支持伪:lastChild – MacroG0D

+0

感谢您试图帮助,但它仍然无法正常工作。也许我没有很好地解释我的问题..我不知道如何解释更好,然后只显示:http://macrogaming.usite.pro/index.html 例如,当用户在命令行中放'是' ,“惠康在船上”等文字必须逐字出现。 (但“机器”必须正常出现)。 – MacroG0D

相关问题