简体   繁体   English

直到我在Firebug中设置断点,jQuery才会执行

[英]jQuery does not execute until I put a break point in Firebug

I am using twig to generate my html stuff and an external js file to call jQuery(document).ready(function() {....}); 我正在使用树枝生成我的html东西和一个外部js文件来调用jQuery(document).ready(function() {....}); This probably is a simple problem but I can't figure it out. 这可能是一个简单的问题,但我无法弄清楚。

Here's my code: base.html.twig 这是我的代码:base.html.twig

<script>
    var currentPage = "{{ path(app.request.attributes.get('_route'),app.request.attributes.get('_route_params')) }}".replace(/[0-9]|\//g, '').toLowerCase().replace('app_dev.php', '');
    $.ajax({
        type: 'GET',
        url: "{{ asset('/app_dev.php/GetMenuJson/') }}" + currentPage,
        success: function(data){
            constructMenuData(data);
        }
    });
    function constructMenuData(menuData)
    {
        var contentForDiv = "";
        //Bunch of other concatenation statements to generate the html
        contentForDiv += "<li><a href=''  class='menuLink'  id='"+result[j][k]["id"]+"'>" + result[j][k]["menuName"] + "</a></li>";
        contentForDiv += "</div>";
        // At the end
        $('#menu').html(contentForDiv);
    }
</script>

In my index.html.twig 在我的index.html.twig中

{% extends "::base.html.twig" %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('bundles/productorderlookup/js/ProductOrderEdit.js') }}" type="text/javascript"></script>

ProductOrderEdit.js ProductOrderEdit.js

jQuery(document).ready(function() {
    $('a.menuLink').click(function (event) {
        event.preventDefault();
        console.log("IN THE FUNCTION");
    })
});

My generated html looks like this 我生成的html看起来像这样

<ul id="menu-item">
    <li>
        <a id="1" class="menuLink" href="">Release</a>
    </li>
    <li>
        <a id="17" class="menuLink" href="">Other Pos</a>
    </li>
    <li>
        <a id="18" class="menuLink" href="">FSC Labels</a>
    </li>
</ul>

Problem: The link when clicked on doesn't work. 问题:单击链接时不起作用。 I have tried wrapping it up in another function explicitly and calling it in the index.html.twig file but it still won't work. 我尝试将其显式包装在另一个函数中,并在index.html.twig文件中调用它,但仍然无法正常工作。 The only time it works is when I put a breakpoint in firebug. 它唯一有效的时间是在Firebug中设置断点时。 I realize this probably is some kind of race condition or some other minor problem but I am not able to fix it. 我意识到这可能是某种竞争状况或其他一些小问题,但我无法解决。 I have tried searching various questions on SO but it wasn't helpful so far. 我曾尝试搜索关于SO的各种问题,但到目前为止还没有帮助。 What might be the problem? 可能是什么问题?

NOTE: I have multiple jQuery(document).ready(function() {...}); 注意:我有多个jQuery(document).ready(function() {...}); all over the page. 整个页面。 Not sure if that matters. 不确定是否重要。

This should be pretty straight. 这应该很简单。

First, having multiple jQuery(document).ready(function() {...}); 首先,具有多个jQuery(document).ready(function() {...}); does not affect any code flow. 不会影响任何代码流。 It might negatively affect your app's performance if you have a lot of them, but the functionality should remain intact. 如果有很多应用程序,则可能会对应用程序的性能产生负面影响,但功能应保持不变。 That is as long as code in one ready does not depend on another ready - that is just plain bad :). 只要一个ready中的代码不依赖于另一个ready -那简直是很糟糕:)。

Now, back to your problem: If I see it right, you send out ajax call that returns json containing menu items which are then converted to html and put into #menu element. 现在,回到您的问题:如果我看对了,您将发出ajax调用,该调用返回包含菜单项的json ,然后将其转换为html并放入#menu元素中。 The problem is that the page has announced ready event long before the items have been returned to your page. 问题在于该页面已宣布ready事件,而早已将项目返回到您的页面。 In turn, your code 反过来,您的代码

$('a.menuLink').click(function (event){ ... });

should not have bound any element - those are not there yet. 不应该绑定任何元素-尚不存在。

The quick fix would be to use delegated bind ( on ) instead of click which binds not only currently present elements but those that will be created in future. 快速解决方案是使用委托绑定( on )代替click ,它不仅绑定当前存在的元素,而且绑定将来将创建的元素。 Assuming the #menu element is always present, you could do: 假设#menu元素始终存在,则可以执行以下操作:

$('#menu').on('click', 'a.menuLink', function(){
    // Here goes your code, same as you originally intended
});

Does this help? 这有帮助吗?

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

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