简体   繁体   English

jQuery load()/事件委托问题

[英]jQuery load()/event delegation Issue

Ok, I've just found out about event delegation, Click Issue: Dynamically Generated Links Not Triggering Click Function my issue here is as follows. 好的,我刚刚发现了事件委托, 单击问题:动态生成的链接未触发单击功能,这里的问题如下。

The UL is loaded in via a jQuery load() call. 通过jQuery load()调用加载UL。

<ul id="activityPaganation" class="paganation">
    <li>1</li>
    <li><a href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">4</a></li>
    <li><a href="#">5</a></li>
    <li><a href="#">6</a></li>
</ul>

The first piece of jQuery will load in the above UL with paganation links. jQuery的第一部分将在上述UL中使用paganation链接加载。 The second part of jquery gets the paganation page number from the paganation link you click, now the issue: jQuery的第二部分从您单击的paganation链接获取paganation页码,现在是问题:

I can't get the the updated page loaded into #accountActivity once you click on a paganation link, is this a delegation issue or is what I'm trying to do just not possible, are the paganation links not able to see #accountActivity as this is where the inital load is loaded into?! 一旦您单击了paganation链接,我无法将更新后的页面加载到#accountActivity ,这是委派问题还是我试图做的事情,paganation链接无法将#accountActivity视为这是初始载荷加载到的地方吗?

$(document).ready(function() {
    if($('#accountActivity')) {
       $('#accountActivity').load('ajax.php?id=3');
    }

    $(document).on("click", "#activityPaganation li a", function(e) {
       e.preventDefault();
       var pid = $(this).text();
       $('#accountActivity').load('ajax.php?id=3&value='+pid);
    });
});

Thanks in advance for all help. 在此先感谢您提供的所有帮助。

$(document).ready(function() {
    if($('#accountActivity')) {
       $('#accountActivity').load('ajax.php?id=3');
    }

    $('#activityPaganation').on("click", "li a", function(e) {
       e.preventDefault();
       var pid = $(this).text();
       $.post('ajax.php',{id:'3',value:pid},function(response) {
            $('#accountActivity').html(response);
       });
    });
});

IF #activityPaganation is not dynamically created. 如果不是动态创建#activityPaganation

I think you have the right idea, just syntax seems a little off to me. 我认为您有一个正确的主意,只是语法对我而言似乎有点不足。 I'm winging it here, and somewhat of a jQuery noob, so bear with me if it doesn't work: 我在这里使用它,有点像jQuery noob,所以如果它不起作用,请多多包涵:

Change the second block of code to : 将第二个代码块更改为:

$('#activityPaganation li a').click(function(e) {
   e.preventDefault();
   var pid = $(this).text();
   $('#accountActivity').load('ajax.php?id=3&value='+pid);
});

I always use alerts() as well to find out if its picking up certain variables. 我也总是使用alert()来确定它是否拾取了某些变量。 Try putting an alert after you assign a value to pid (ie: alert(pid);). 在为pid分配值后尝试放置警报(即:alert(pid);)。 If it's blank, you know it's not being assigned properly. 如果为空,则表示未正确分配。 If it doesn't show up at all, there is trouble before it. 如果它根本不显示,那就有麻烦了。

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

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