简体   繁体   English

此代码后,Javascript停止工作

[英]Javascript stops working after this code

I have written this code for a cmd console-like typing effect. 我已经为类似cmd控制台的键入效果编写了此代码。 The code itself works fine but any code I place after it doesn't work. 该代码本身可以正常工作,但是我放置的所有代码都无法正常工作。 I've been trying to figure it out for a while now and I'm pretty stuck. 我已经尝试了好一阵子了,我很困。

Here's a JSFiddle of the problem. 这是问题的JSFiddle。 http://jsfiddle.net/GWj4b/ http://jsfiddle.net/GWj4b/

/**
 * CONSOLE
 */

setInterval(cursorBlink, 500);
function cursorBlink() {
    if($('#cursor').css('visibility') == 'hidden'){
        $('#cursor').css({'visibility': 'visible'});
    } else {
        $('#cursor').css({'visibility': 'hidden'});
    }
}

var text = $('#type').html();
var text = text.replace(/<br>/g, '_');

$('#type').html('');

$.each(text.split(''), function(i, letter) {
    setTimeout(function(){
        if(letter == '_'){
            $('#type').html($('#type').html() + '<br />');
        } else {
            $('#type').html($('#type').html() + letter);
        }
    }, 100 * i);
});

You're not using HTML right? 您使用的不是HTML吗?

after

$('#type') returns undefined , and then you do text.replace(/<br>/g, '_'); $('#type')返回undefined ,然后执行text.replace(/<br>/g, '_');

you cannot do undefined.replace , so it fails, and the execution stops. 您不能执行undefined.replace ,因此它将失败,并且执行将停止。

Try to add the html code for that JS code 尝试为该JS代码添加html代码

Also, you must add JQuery to your fiddle, in the Frameworks & Extensions part. 同样,您必须在Frameworks&Extensions部分中将JQuery添加到您的小提琴中。

See this fiddle http://jsfiddle.net/9748L/ 看到这个小提琴http://jsfiddle.net/9748L/

If you look in the console log you see: Uncaught TypeError: Cannot call method 'replace' of undefined 如果您在控制台日志中查看,则会看到: Uncaught TypeError: Cannot call method 'replace' of undefined

text is not defined when you call replace on it because the element you're looking for doesn't exist. 调用replace时未定义text,因为要查找的元素不存在。

You can test to see if it exists like this 您可以测试以查看它是否存在

if(text) {
  text.replace(/<br>/g, '_');
}

Javascript will stop execution if it hits a runtime error like this. 如果Javascript遇到这样的运行时错误,它将停止执行。

Consider learning more about debugging this type of issue with Chrome's dev tools or an equivalent in your browser. 考虑更多地了解如何使用Chrome的开发工具或浏览器中的等效工具调试此类问题。 Once you look to see the error, its pretty clear what line is causing the issue. 一旦您看到该错误,就会很清楚是什么引起了该问题。

Incidentally, after adding the elements that you are trying to reference, the code executes through. 顺便说一句,在添加您要引用的元素之后,代码将通过执行。 If they might not exist though, you need to do the checking in code, and its a good habit regardless. 如果它们可能不存在,则需要进行代码检入,无论如何,这都是一个好习惯。

http://jsfiddle.net/GWj4b/1/ http://jsfiddle.net/GWj4b/1/

我刚刚调试了它,看起来文本是未定义的,所以也许您还没有在html中创建该元素。

var text = text.replace(/<br>/g, '_');

Indeed you are missing some html code and the text you try to manipulate is undefined . 确实,您缺少一些html代码,并且您尝试操作的文本是undefined You could add the following html code, 您可以添加以下html代码,

<span id="type">is this what you try to do????</span><span id="cursor">_</span>

to achieve the effect you are aiming for. 达到您想要的效果。

http://jsfiddle.net/JKp5z/ http://jsfiddle.net/JKp5z/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM