简体   繁体   English

加载时在html上写

[英]Write on html on load

I have created a function who tracks on which slide I am currently on and display the result eg If I am on slide 2 of 3 it will display 2/3 我创建了一个函数,可以跟踪我当前在哪张幻灯片上并显示结果,例如,如果我在第2张幻灯片中,则显示3/2

my problem is that right now it is set to do that every time I click the forward arrow but it displays nothing on page load. 我的问题是,现在将其设置为每次单击前进箭头时都执行此操作,但是页面加载时什么都不显示。

$('.forward').click(function() {
var current = $('#slider').data('AnythingSlider').currentPage; // returns page #
var count = $("#slider").children().length - 2;
$("#bottom-image").html(current + "/" + count) ;
});

I am trying to find out how to execute this function on page load and where to put it in my code. 我试图找出如何在页面加载时执行此功能以及将其放置在我的代码中的位置。 I am currently learning Javascript through Codecadamedy so I have a basic knowledge of Javascript but I am not enough fluent right now to figure this one out. 我目前正在通过Codecadamedy学习Javascript,因此我对Javascript有基本的了解,但是现在我还不太流利,无法弄清楚这一点。

Here is a link to the current non working code : http://www.soleilcom.com/metacor_dev/our-plants.php 这是当前无效代码的链接: http : //www.soleilcom.com/metacor_dev/our-plants.php

It looks like you are using jQuery. 看起来您正在使用jQuery。 To execute a function on DOM load in query, do this: 要在查询中的DOM加载上执行功能,请执行以下操作:

$(document).ready(function() {
    /* your code */
});

In your case, that would be: 您的情况是:

$(document).ready(function() {
    $('.forward').click(function() {
        var current = $('#slider').data('AnythingSlider').currentPage; // returns page #
        var count = $("#slider").children().length - 2;
        $("#bottom-image").html(current + "/" + count) ;
    });
});

For things like most event handlers, and most other things, initializing at DOM load is good enough. 对于大多数事件处理程序之类的事物以及大多数其他事物,以DOM加载进行初始化就足够了。 If your code needs to take account for rendered elements or rendered heights, use $(window).load() instead. 如果您的代码需要考虑渲染的元素或渲染的高度,请改用$(window).load() (In your case DOM load is fine). (在您的情况下,DOM加载很好)。

Note that this will just establish the click handler at load time. 请注意,这只会在加载时建立点击处理程序。 To also run it once, you can do it automatically by either calling the function yourself or triggering a click. 要同时运行一次,您可以通过自己调用函数或触发点击来自动执行。 To call it yourself, first define another function. 要自己调用它,首先定义另一个函数。 The use the function in both the click handler and in one immediate call: 在点击处理程序和一次直接调用中使用该函数:

$(document).ready(function() {
    var forward = function() {
        var current = $('#slider').data('AnythingSlider').currentPage; // returns page #
        var count = $("#slider").children().length - 2;
        $("#bottom-image").html(current + "/" + count) ;
    }

    $('.forward').click(forward);
    forward();
});

Or to trigger it yourself, just define the click handler and trigger a click programatically: 或自行触发,只需定义点击处理程序并以编程方式触发点击即可:

$(document).ready(function() {
    $('.forward').click(function() {
        var current = $('#slider').data('AnythingSlider').currentPage; // returns page #
        var count = $("#slider").children().length - 2;
        $("#bottom-image").html(current + "/" + count) ;
    }).click();
});

It looks like you are using jQuery, so you would use this: 看起来您正在使用jQuery,因此可以使用以下代码:

$(document).ready(function(){
    // Code here
});

Or, you can use the shortcut: 或者,您可以使用快捷方式:

$(function(){
    // Code here
});

Read more about this on the jQuery website jQuery网站上阅读有关此内容的更多信息

If you give the function a name, then you can use it multiple times: 如果给函数命名,则可以多次使用它:

$(document).ready(function() {
    // Bind to click event
    $('.forward').click(forwardSlide)

    // Execute function on page load
    forwardSlide();
});

function forwardSlide() {
    var current = $('#slider').data('AnythingSlider').currentPage; // returns page #
    var count = $("#slider").children().length - 2;
};

Is this what you're looking for? 这是您要找的东西吗?

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

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