简体   繁体   English

如何在页面加载时运行函数,然后在单击后运行?

[英]How can I run a function on page load and then after click?

hi this is my very first time using jquery on any project at all.嗨,这是我第一次在任何项目上使用 jquery。 I am needing a little help if someone can如果有人可以的话,我需要一点帮助

on my page i have some code to load a json file this file contains menu details for the main menu such as在我的页面上,我有一些代码可以加载 json 文件,该文件包含主菜单的菜单详细信息,例如

name / action / poster名称/动作/海报

this is my code这是我的代码

var Items = "";
var flixAPI = "https://example.com/api.php?action=MAIN";

$.getJSON(flixAPI, function (data) {

    $.each(data, function (i, item) {
        Items += "<div class='img'>";
        Items += "<a target='_blank' href='" + item.ACTION + "'>";
        Items += "<img src='"+ item.POSTER +"' alt='" + item.NAME + "'>";
        Items += "</a>";
        Items += "<div class='desc'>" + item.NAME.substr(0, 16) + "</div>";
        Items += "</div>";
    });

    $('#content').html(Items);

});

i can get this code working when i place it into the document ready.当我将它放入准备好的文档时,我可以让这段代码工作。

my issue is after the main menu is populated and added to the page i then need to stop the page redirecting when a href link is clicked and get the value of the href link to then send another get json request to load the next page我的问题是在主菜单填充并添加到页面后,我需要在单击 href 链接时停止页面重定向并获取 href 链接的值,然后再发送另一个 get json 请求以加载下一页

i have tried loading the main menu on the document ready and then sticking one call into a .click binding to load the next set of items based on the href link clicked but when i try do this inside or outside the document ready the .click binded function wont work我尝试加载文档上的主菜单准备好,然后将一个调用粘贴到 .click 绑定中以根据单击的 href 链接加载下一组项目,但是当我尝试在文档内部或外部执行此操作时,.click 绑定功能不起作用

Put the code in a named function so you can call it from both places.将代码放在命名函数中,以便您可以从两个地方调用它。

 function getFlix(flixAPI) { var Items = ""; $.getJSON(flixAPI, function(data) { $.each(data, function(i, item) { Items += "<div class='img'>"; Items += "<a class='menu-link' target='_blank' href='" + item.ACTION + "'>"; Items += "<img src='" + item.POSTER + "' alt='" + item.NAME + "'>"; Items += "</a>"; Items += "<div class='desc'>" + item.NAME.substr(0, 16) + "</div>"; Items += "</div>"; }); $('#content').html(Items); }); } $(document).ready(function() { getFlix("https://example.com/api.php?action=MAIN"); $("#content").on("click", ".menu-link", function(e) { e.preventDefault(); getFlix(this.href); }); });

This uses event delegation because the menu links are added dynamically.这使用了事件委托,因为菜单链接是动态添加的。 See Event binding on dynamically created elements?请参阅动态创建的元素上的事件绑定?

You would need to identify the menu items with either classes or id's.您需要使用类或 id 来标识菜单项。

Then you would have to define the onClick event for each one of those who you need to fetch the JSON for.然后,您必须为需要为其获取 JSON 的每个人定义 onClick 事件。

ON the onClick event, you need to use e.stoppropagation or e.preventdefault (check the usage online) and then use ajax to fetch the JSON and populate whatever you are populating.在 onClick 事件上,您需要使用 e.stoppropagation 或 e.preventdefault(在线检查使用情况),然后使用 ajax 获取 JSON 并填充您要填充的任何内容。

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

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