如何删除已添加到带有+ =的段落中的文本

时间:2015-05-08 11:50:18

标签: javascript

我第一次问这个时搞砸了。但是如何通过+ =删除添加到元素的文本。我的javascript当前看起来像这样:

printLetterByLetter(destination, message, speed){
    var i = 0;
    var interval = setInterval(function(){
        document.getElementById(destination).innerHTML += message.charAt(i);
        i++;
        if (i > message.length){
        clearInterval(interval)
        }
    }, speed);
}
function clear(destination){
document.getElementById(destination).innerHTML=""
}

和我的HTML:

<p id="storyStarter" onclick="document.getElementById(&quot;storyStarter&quot;).innerHTML=&quot;&quot;; printLetterByLetter(&quot;storyStarter&quot;,&quot; It all begins here&quot;,50)">Click To begin Story</p>
<a onclick="clear(&quot;storyStarter&quot;)">Clear Text</a>

它会打印到页面,但点击卸妆器不会删除文本。

3 个答案:

答案 0 :(得分:4)

这是因为名称ptrdiff_t已用于浏览器中的内容,可能是document.clear method

如果您为该功能使用不同的名称,它可以正常工作:

&#13;
&#13;
clear
&#13;
function printLetterByLetter(destination, message, speed){
    var i = 0;
    var interval = setInterval(function(){
        document.getElementById(destination).innerHTML += message.charAt(i);
        i++;
        if (i > message.length){
        clearInterval(interval)
        }
    }, speed);
}

function empty(destination){
  document.getElementById(destination).innerHTML=""
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

只需更改点击时调用的函数名称,例如..

<a onclick="clearText(&quot;storyStarter&quot;)">Clear Text</a>

function clearText(destination){
   document.getElementById(destination).innerHTML=""
}

答案 2 :(得分:0)

功能名称clear存在问题。我认为这是一个保守的词。只需更改名称即可。

<强> HTML

<p id="storyStarter" onclick="document.getElementById('storyStarter').innerHTML='';printLetterByLetter('storyStarter','It all begins here',50)">Click To begin Story</p>

明文

<强> JAVASCRIPT

function printLetterByLetter(destination, message, speed){
var i = 0;
var interval = setInterval(function(){
    document.getElementById(destination).innerHTML += message.charAt(i);
    i++;
    if (i > message.length){
    clearInterval(interval)
    }
 }, speed);
}
function clearing(destination){
    document.getElementById(destination).innerHTML=""
}

DEMO