Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击链接时,如何使其自身更新?_Javascript_Jquery - Fatal编程技术网

Javascript 单击链接时,如何使其自身更新?

Javascript 单击链接时,如何使其自身更新?,javascript,jquery,Javascript,Jquery,我有下一个链接,可以让用户浏览URL列表(存储在数组中)。当用户单击“下一步”时,我希望为我的阵列中的下一个网站更新链接href和锚点。到目前为止,我尝试过的方法是,每次它都会跳过一个URL,我知道为什么,它与上一次的点击有关,甚至没有完成,但正确的方法是什么 下面是一个示例,您可以清楚地看到我所说的内容: $(文档).ready(函数(){ var项目=[]; $.each(URL、函数(i、项){ items.push(“”+item['name']+“”); }); $('#list')

我有下一个链接,可以让用户浏览URL列表(存储在数组中)。当用户单击“下一步”时,我希望为我的阵列中的下一个网站更新链接href和锚点。到目前为止,我尝试过的方法是,每次它都会跳过一个URL,我知道为什么,它与上一次的点击有关,甚至没有完成,但正确的方法是什么

下面是一个示例,您可以清楚地看到我所说的内容:


$(文档).ready(函数(){
var项目=[];
$.each(URL、函数(i、项){
items.push(“
  • ”+item['name']+“
  • ”); }); $('#list').append(items.join(''); changeNextLink(0); $(“#frame”).attr('src',url[0]['url']); }); 变量url=[{“名称”:“易趣”,“url”:”http://ebay.com},{“名称”:“亚马逊”,“url”:http://amazon.com},{“名称”:“msn”,“url”:http://msn.com},{“名称”:“雅虎”,“url”:http://yahoo.com},{“名称”:“维基百科”,“url”:http://wikipedia.org"}]; 函数changeNextLink(x){ $('#now').html(URL[x]['name']);//显示当前加载页面的名称 $('#nextLink').html(URL[x+1]['name']);//作为下一链接锚定的下一个网站的名称 $('#nextLink').attr('href',url[x+1]['url']);//指向下一个网站的url $(“#nextLink”).attr('onclick','changeNextLink('+(x+1)+');//问题点。准备下一个链接,以便下次单击。 } 现在:|下一步:

    如何添加一个新的var来跟踪下一个link num,并为
    #nextLine
    添加一个单击处理程序:

    $(document).ready(function(){
        var nextLinkNum = 0;
        var items = [];
        $.each(urls, function(i, item) {
            items.push("<li>"+item['name']+"</li>");
        });
    
        $('#list').append(items.join(''));
    
        changeNextLink(nextLinkNum);
        $("#frame").attr('src', urls[0]['url']);
    
        $('#nextLink').click(function() { 
            if (nextLinkNum + 1 < urls.length)
                changeNextLink(++nextLinkNum); 
        });
    
    });
    
    function changeNextLink(x){     
        $('#now').html(urls[x]['name']); //show the name of currently loaded page
        $('#nextLink').html(urls[x+1]['name']); //name of next website as anchor of next link
        $('#nextLink').attr('href', urls[x+1]['url']); //url to next website
        $('#nextLink').attr('onclick','changeNextLink('+(x+1)+')'); //the problem spot. prepare next link for next click.
    }
    
    $(文档).ready(函数(){
    var nextLinkNum=0;
    var项目=[];
    $.each(URL、函数(i、项){
    items.push(“
  • ”+item['name']+“
  • ”); }); $('#list').append(items.join(''); changeNextLink(nextLinkNum); $(“#frame”).attr('src',url[0]['url']); $('#nextLink')。单击(函数(){ if(nextLinkNum+1
    我对您的javascript做了一些细微的更改,现在它似乎工作正常


    首先,它将检查列表中是否有下一个url,如果没有,下一个url将设置为当前url(如果需要,可以将其设置回第一个)。如果列表中还有其他url,则将其设置为下一个url。

    @AdamRackis我喜欢此解决方案的优雅。我很可能会同意这个。谢谢。@AdamRackis我在执行此操作时遇到了问题。让我更新jsBin并发布它。@ofko-我真的不确定。在评论线程中很难理解这样的事情。你可能想把这些都收集起来,然后把它作为一个新问题发布。我想这会给你带来最好的结果。抛开我之前的评论,你能在你的代码中添加changeNextLink函数来完成你的答案吗?当前函数在与代码一起使用时无法正常工作。您可以在此处看到结果:(工作不正常)我会发布一个新问题,但它可能会被标记为重复。因此,这里的问答部分目的是与公众分享您所做的更改及其原因,而不仅仅是粘贴到代码链接中。
    $(document).ready(function(){
        var nextLinkNum = 0;
        var items = [];
        $.each(urls, function(i, item) {
            items.push("<li>"+item['name']+"</li>");
        });
    
        $('#list').append(items.join(''));
    
        changeNextLink(nextLinkNum);
        $("#frame").attr('src', urls[0]['url']);
    
        $('#nextLink').click(function() { 
            if (nextLinkNum + 1 < urls.length)
                changeNextLink(++nextLinkNum); 
        });
    
    });
    
    function changeNextLink(x){     
        $('#now').html(urls[x]['name']); //show the name of currently loaded page
        $('#nextLink').html(urls[x+1]['name']); //name of next website as anchor of next link
        $('#nextLink').attr('href', urls[x+1]['url']); //url to next website
        $('#nextLink').attr('onclick','changeNextLink('+(x+1)+')'); //the problem spot. prepare next link for next click.
    }