javascript - 从外部链接每 5 秒更改一次文本

标签 javascript jquery dynamic text external

我找到了一个可用的 jQuery,可以随着时间的推移动态更改文本,并具有淡入淡出效果。 我希望从外部链接自动添加这些词:Words Page .

我该怎么做?

我的目标是保持 JS 文件小/最小,因为我有更多其他 jQuery 代码。

感谢任何指导!

检查我的:enter link description here

HTML

<span class="textbox">Stylish</span>

CSS

span.textbox{
    display: inline;
    float: left;
    font-size: 20px;
    margin-top: 50px;
    margin-left: 50px;
    color: #131313;
    font-weight: bold;
    letter-spacing: 1px;
}

JS

$(document).ready(function() {

   // List your words here:
var words = [
    'Awesome',
    'Special',
    'Sofisticated',
    'Simple',
    'Great',
    'Better',
    'Stronger',
    'Stylish',
    'Flawless',
    'Envied',
    'Strong',
    'Sucessful',
    'Grow',
    'Innovate',
    'Global',
    'Knowledgable',
    'Solid'
    ], i = 0;

setInterval(function(){
    $('.textbox').fadeOut(500, function(){
        $(this).html(words[i=(i+1)%words.length]).fadeIn(500);
    });
}, 5000);

});

最佳答案

使用 jQuery.ajax()

$.ajax('http://www-958.ibm.com/software/data/cognos/manyeyes/datasets/5-adjectives-to-describe-company-2/versions/1.txt').done(function(response, status) {
    // Check for error result (404, 500, etc)
    if (status !== 'error')
    {
        // Split words using ", " as separator to words array.
        var words = response.split(", ");
        // Your code here to change texts. Nothing changed.
        setInterval(function(){
            $('.textbox').fadeOut(500, function(){
                $(this).html(words[i=(i+1)%words.length]).fadeIn(500);
            });
        }, 5000);
    }
});

但是由于跨域错误,您将无法获取该网址。

将文本复制到您的域或进行重定向。

关于javascript - 从外部链接每 5 秒更改一次文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22722011/

相关文章:

javascript - 使用具有用户定义字段的新请求者创建新的 Zendesk 票证

javascript - 保存为 cookie 时,JSON 编码数组转换为字符串

javascript - 如何在 AngularJS 中访问预定义数组

javascript - 为什么IE在使用ajax时不喜欢越南字符?

javascript - 单击 anchor 标记后将单选按钮标记为选中

javascript - 单击事件处理程序仅偶尔附加到 DOM

C++ 动态 boolean 数组导致崩溃

javascript - 在 JavaScript 中动态引用数组

javascript - 滑动检测和点击检测

c++ - 在 C 程序中动态加载外部模块?