V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
Howcouleyoubeso
V2EX  ›  JavaScript

有关 js 的打字机效果问题

  •  1
     
  •   Howcouleyoubeso · 2016-03-21 16:28:17 +08:00 · 1693 次点击
    这是一个创建于 2962 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我是刚学 js 的新手,想在网页上实现打字机效果,但是却没成功,请各位帮忙看一下,谢谢。
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    <div id="autotype">This is a test</div>
    <script type="text/javascript">
    $.fn.autotype = function() {
    var $tt = $(this);
    var str = $tt.html();
    var index = 0;
    $(this).html('');
    var timer = setInterval(function() {
    var current = str.substr(index, 1);

    if (current == '<')
    index = str.indexOf('>', index) + 1;
    else
    index++;

    $tt.html(str.substring(0, index) + (index & 1 ? '_' : ''));

    if (index >= str.length)
    clearInterval(timer);
    }, 55);
    };

    $("#autotype").autotype();

    </script>
    </body>
    </html>
    2 条回复    2016-03-23 10:57:43 +08:00
    wyc829
        1
    wyc829  
       2016-03-23 10:11:54 +08:00   ❤️ 1
    有效果啊,就是要加上 jquery 就是了.
    Howcouleyoubeso
        2
    Howcouleyoubeso  
    OP
       2016-03-23 10:57:43 +08:00
    @wyc829 请问怎么加?$("#autotype").autotype(); 我不是已经调用函数了吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   893 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 22:45 · PVG 06:45 · LAX 15:45 · JFK 18:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.