热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何重新加载javascript而不是页面-Howtoreloadjavascriptbutnotpage

Ihaveaproblemwithaslider.WhenIloadthefirsttimemyWebpage,itshowsasliderwithsome

I have a problem with a slider. When I load the first time my Web page, it shows a slider with some images. I have some links to change the images to show, so when I click, I want to display as a slider the other ones. The problem is that when I click on the new link, it doesn't use the jquery function to format the slider.

我有滑块问题。当我第一次加载我的网页时,它会显示带有一些图像的滑块。我有一些链接可以更改要显示的图像,所以当我点击时,我想显示其他的滑块。问题是当我点击新链接时,它不使用jquery函数来格式化滑块。

Here I paste some code so you can figure out:

在这里,我粘贴一些代码,以便您可以弄清楚:

  • Dormitorio azul turquesa.
  • Escaparate Agosto 2011.
  • Escaparate Enero 2011.

And then the jquery:

然后是jquery:

$(document).ready(function()
{
    var timeout, manualMode = false,

            $slideshow = $('#slideshow'),
    $items = $slideshow.find('li').hide(),
    total = $items.length,
    getItem = function($item, trav) {
        var $returnItem = $item[trav]();
        return $returnItem.length ?
            $returnItem :
            $items[(trav == 'next') ? 'first' : 'last']();
    },

    showItem = function($currentItem, $itemToShow) {
        var $itemToShow =
            $itemToShow || getItem($currentItem,'next');

        $currentItem.fadeOut(300, function() {
            $itemToShow.fadeIn(300, fadeCallback);
        });
    },

    fadeCallback = function() {
        if (manualMode) { return; }

        var $this = $(this),
            $next = getItem($this, 'next'),
            num = $this.prevAll().length + 1;


        // set the timeout for showing
        // the next item in 5 seconds
        timeout = setTimeout(function() {
            showItem($this, $next);
        }, 3000);
    },

    handleBtnClick = function(e) {
        clearTimeout(timeout);

        manualMode = true;

        var $currentItem = $items.filter(':visible'),
            $itemToShow = getItem($currentItem, e.data.direction);

        showItem($currentItem, $itemToShow);
    };

    $items.eq(0).fadeIn(500, fadeCallback);

});

This works perfect for the first time I load the page, but when I change the content with this:

这在我第一次加载页面时非常有用,但是当我用这个更改内容时:


So basically, what I do is, after an onclick() event on some link, I change the html content of a div, but obviously, it doesn't use the jquery, as it wasn't loaded at first. Any idea on how to solve this?

所以基本上,我做的是,在某个链接上的onclick()事件之后,我更改了div的html内容,但显然,它不使用jquery,因为它最初没有加载。关于如何解决这个问题的任何想法?

If I use location.reload(); it shows the first items I had inside, so it doesn't work for me...

如果我使用location.reload();它显示了我里面的第一件物品,所以它对我不起作用......

EDIT:
Wait, I edit the jsFiddle...

编辑:等等,我编辑jsFiddle ...

1 个解决方案

#1


1  

You need to create an init method for your slideshow and recall that when you need to.

您需要为幻灯片创建一个init方法,并在需要时调用它。

I want to gag seeing how this js is set up so I will ignore it and post how I would have handled it:

我想看看如何设置这个js所以我会忽略它并发布我将如何处理它:

$(document).ready(function(){
    slideshow.init();

    $('#nuestra_tienda_show').on('click', function()
    {
        $('.containerGaleria').html('');
        $('
  • Dormitorio azul turquesa.
  • Escaparate Agosto 2011.
  • Escaparate Enero 2011.
  • Escaparate Junio 2011.
') .prependTo('.containerGaleria'); $('#galeria_seleccionada').html('Nuestra tienda 2011'); slideshow.init(); }); }); var slideshow = { timeout: null, manualMode: false, total: 0, init: function(){ clearTimeout(this.timeout); var items = $('#slideshow').find('li').hide(); this.total = items.length; items.eq(0).fadeIn(500, this.fadeCallback); }, getItem: function(item, trav) { var returnItem = item[trav](); return returnItem.length ? returnItem : items[(trav == 'next') ? 'first' : 'last'](); }, showItem: function(currentItem, itemToShow) { var itemToShow = itemToShow || this.getItem(currentItem,'next'); currentItem.fadeOut(300, function() { itemToShow.fadeIn(300, this.fadeCallback); }); }, fadeCallback: function() { if (manualMode) { return; } var $this = $(this), next = getItem($this, 'next'), num = $this.prevAll().length + 1; // set the timeout for showing // the next item in 5 seconds timeout = setTimeout(function() { this.showItem($this, next); }, 3000); }, handleBtnClick: function(e) { clearTimeout(this.timeout); this.manualMode = true; var currentItem = items.filter(':visible'), itemToShow = this.getItem(currentItem, e.data.direction); this.showItem(currentItem, itemToShow); }, handleBtnClick: function(e) { clearTimeout(this.timeout); this.manualMode = true; var currentItem = items.filter(':visible'), itemToShow = this.getItem(currentItem, e.data.direction); this.showItem(currentItem, itemToShow); }; };

Then you would just call slideshow.init() when you want to re-evaluate the slideshow content.

然后,当您想要重新评估幻灯片显示内容时,您只需调用slideshow.init()。

I didn't thoroughly audit this but the general idea is to turn this massive chain of vars into a single js object and contain the whole thing.

我没有彻底审核这个,但一般的想法是将这个庞大的变量链变成一个单独的js对象并包含整个事物。

I cannot understand why you would write the contents of the slideshow like this in the click function. You're wiping the html of the container, creating the elements, then prepending them to the container and rewriting the title id with the same string over and over. Why not just set the html of the slideshow to the new images?

我不明白为什么你会在点击功能中写这样的幻灯片的内容。您正在擦除容器的html,创建元素,然后将它们预先添加到容器中,并反复使用相同的字符串重写标题ID。为什么不直接将幻灯片的html设置为新图像?

$('#slideshow').html('
  • Dormitorio azul turquesa.
  • Escaparate Agosto 2011.
  • Escaparate Enero 2011.
  • Escaparate Junio 2011.
  • ');

    推荐阅读
    • 浅解XXE与Portswigger Web Sec
      XXE与PortswiggerWebSec​相关链接:​博客园​安全脉搏​FreeBuf​XML的全称为XML外部实体注入,在学习的过程中发现有回显的XXE并不多,而 ... [详细]
    • Nginx使用AWStats日志分析的步骤及注意事项
      本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
    • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
    • CSS3选择器的使用方法详解,提高Web开发效率和精准度
      本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
    • android listview OnItemClickListener失效原因
      最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
    • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
    • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
    • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
    • web.py开发web 第八章 Formalchemy 服务端验证方法
      本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
    • SpringBoot整合SpringSecurity+JWT实现单点登录
      SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
    • Asp.net Mvc Framework 七 (Filter及其执行顺序) 的应用示例
      本文介绍了在Asp.net Mvc中应用Filter功能进行登录判断、用户权限控制、输出缓存、防盗链、防蜘蛛、本地化设置等操作的示例,并解释了Filter的执行顺序。通过示例代码,详细说明了如何使用Filter来实现这些功能。 ... [详细]
    • python3 nmap函数简介及使用方法
      本文介绍了python3 nmap函数的简介及使用方法,python-nmap是一个使用nmap进行端口扫描的python库,它可以生成nmap扫描报告,并帮助系统管理员进行自动化扫描任务和生成报告。同时,它也支持nmap脚本输出。文章详细介绍了python-nmap的几个py文件的功能和用途,包括__init__.py、nmap.py和test.py。__init__.py主要导入基本信息,nmap.py用于调用nmap的功能进行扫描,test.py用于测试是否可以利用nmap的扫描功能。 ... [详细]
    • 大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记
      本文介绍了大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记,包括outputFormat接口实现类、自定义outputFormat步骤和案例。案例中将包含nty的日志输出到nty.log文件,其他日志输出到other.log文件。同时提供了一些相关网址供参考。 ... [详细]
    • 获取时间的函数js代码,js获取时区代码
      本文目录一览:1、js获取服务器时间(动态)2 ... [详细]
    • 开发笔记:图像识别基于主成分分析算法实现人脸二维码识别
      篇首语:本文由编程笔记#小编为大家整理,主要介绍了图像识别基于主成分分析算法实现人脸二维码识别相关的知识,希望对你有一定的参考价值。 ... [详细]
    author-avatar
    黄欣豪972
    这个家伙很懒,什么也没留下!
    PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有