jQuery-.Ready在不在页面上的div标签上触发 [英] jQuery - .Ready fires on div tag not in the page

查看:61
本文介绍了jQuery-.Ready在不在页面上的div标签上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在单独的.js脚本文件中实现了以下功能,该脚本文件已在我的主页(Asp.net mvc)中加载.

I have implemented below function in a separate .js script file which I load in my main page (Asp.net mvc).

回叫服务器的成本很高,但如果需要在需要填充我的div标签的子页面/内容页面上有特殊要求,则可以.

The call back to server is rather costly but ok if specifically requested on the sub-page / content page on which my div tag needs to be filled.

我的问题是代码在每个页面(包括不存在div标签的页面)上运行.

My problem is that the code runs on every page, including pages where the div tag is not present.

我该如何纠正我的代码?

How can I remedy my code?

欢迎任何评论, 谢谢,安德斯,丹麦

Any comments welcome, Thanks, Anders, Denmark

jQuery("divStatWrapper").ready(function()
    { 
    jQuery.getJSON("/Statistics/GetPointsDevelopment", 
        function(json)
        {
            jQuery("#divStatWrapper #divStatLoading").fadeOut(1000);

           var jsonPoints = new Array();

            jQuery.each(json.Points, function(i, item)
            {
                jsonPoints.push(item.PlayerPoints );
            });

            jsonPoints.reverse();

           var api = new jGCharts.Api();
            jQuery('<img>') 
            .attr('src', api.make({
                data : jsonPoints,
                type : "lc",
                size : "600x250"
                })) 
            .appendTo("#divStatContents"); 

            jQuery("#divStatWrapper #divStatContents").fadeIn(1000);

        });
    });

html:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

    <%@ Import Namespace="AJF.Op.Web.MVC.SpilMerePool.Helpers" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <center>
            <table id="statisticsTable" classname="statisticsTable">
                <tr valign="top" align="center">
                    <td>
                        <%Html.RenderPartial(SMPControls.QueueStatus.ToString(), null); %>
                    </td>
                    <td>
                        <h2>
                            Statistik - udvikling i point over tid</h2>
                        <h3>
                            (Alle resultater frem til løbende dato)</h3>
                        <div id="divStatWrapper">
                            <div id="divStatLoading">
                                <img src="../../Images/ajax-loader.gif" />
                                <span>Loading</span>
                            </div>
                            <div id="divStatContents">
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </center>
    </asp:Content>

推荐答案

一个想法:

$("#divStatContents").each(function(){
   // your code here
});

每个#divStatContents将运行一次

Will run once for each #divStatContents

这篇关于jQuery-.Ready在不在页面上的div标签上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆