jQuery getJSON:点击“this”从几个层次往下

发布于 2024-12-01 14:47:11 字数 2070 浏览 2 评论 0原文

摘要:尝试从多次调用中引用点击“this”。

大家好,

我有一种情况,我有一组无序列表形式的按钮(按钮 1 - 6)。单击其中一个按钮时,我希望它 getJSON 加载更多

  • 单击的按钮下方的选项(基于 json 数据)。现在,当我单击其中一个按钮时,它会在所有按钮下加载基于 JSON 的选项。问题是我不确定如何引用“click”函数调用的“this”而不是与嵌套调用之一关联的“this”。
  • 我对 jQuery 很感兴趣,所以我意识到这可能是一个简单的解决方案。如果我的术语有点不对劲,请原谅我。

    感谢您阅读并考虑我的问题。

    这是关联的 javascript:

    $(".block-buttons .level1 a.level1_a").click(function () 
    {
      $.getJSON('api/1/test.json', 
        function(data) {    
            ul = $('<ul>');
            $.each(data.items,
            function( intIndex, objValue )
            {
                a = $("<a/>").text(objValue.name);
                li = $('<li>').attr("class",'level2').append(a);
                ul.append(li);
            });
            /* Here is where the problem lies.  I'd like to append the 
               new JSON-loaded data to the ".level1" element that was clicked
               and not to all of the ".level1" elements.  I need to figure out how 
               to replace the ".level1" below with some reference to the "this"
               from the clicked element.  
            */
            $(".level1").append(ul);            
          });   
      }
      return false; 
    });
    

    这是关联的 HTML:

    <div class="block-center block-buttons">
    <ul>
    <li class="top"><a class="top_a" href="">Make a Selection</a>
        <ul>
        <li class="level1"><a class="level1_a" id="button_1" href="">Button 1</a></li>
        <li class="level1"><a class="level1_a" id="button_2" href="">Button 2</a></li>
        <li class="level1"><a class="level1_a" id="button_3" href="">Button 3</a></li>
        <li class="level1"><a class="level1_a" id="button_4" href="">Button 4</a></li>
        <li class="level1"><a class="level1_a" id="button_5" href="">Button 5</a></li>
        <li class="level1"><a class="level1_a" id="button_6" href="">Button 6</a></li>
        </ul>
    </li>
    </ul>
    </div>
    

    Summary: Trying to reference clicked "this" from several calls deep.

    Hi All,

    I have a situation where I have a set of buttons (Button 1 - 6) in the form of an unordered list. When one of the buttons is clicked, I'd like it to getJSON load some more <li> options (based on the json data) underneath the button that was clicked. Right now, when I click one of the buttons, it loads the JSON-based options under all of the buttons. The problem is that I'm not sure how to reference the "this" of the "click" function call rather than the "this" associated with one of the nested calls.

    I'm a biginner when it comes to jQuery, so I relize that this is probably an easy fix. Forgive me if my terminology is off a bit.

    Thank you for reading and for considering my question.

    Here is the associated javascript:

    $(".block-buttons .level1 a.level1_a").click(function () 
    {
      $.getJSON('api/1/test.json', 
        function(data) {    
            ul = $('<ul>');
            $.each(data.items,
            function( intIndex, objValue )
            {
                a = $("<a/>").text(objValue.name);
                li = $('<li>').attr("class",'level2').append(a);
                ul.append(li);
            });
            /* Here is where the problem lies.  I'd like to append the 
               new JSON-loaded data to the ".level1" element that was clicked
               and not to all of the ".level1" elements.  I need to figure out how 
               to replace the ".level1" below with some reference to the "this"
               from the clicked element.  
            */
            $(".level1").append(ul);            
          });   
      }
      return false; 
    });
    

    Here is the associated HTML:

    <div class="block-center block-buttons">
    <ul>
    <li class="top"><a class="top_a" href="">Make a Selection</a>
        <ul>
        <li class="level1"><a class="level1_a" id="button_1" href="">Button 1</a></li>
        <li class="level1"><a class="level1_a" id="button_2" href="">Button 2</a></li>
        <li class="level1"><a class="level1_a" id="button_3" href="">Button 3</a></li>
        <li class="level1"><a class="level1_a" id="button_4" href="">Button 4</a></li>
        <li class="level1"><a class="level1_a" id="button_5" href="">Button 5</a></li>
        <li class="level1"><a class="level1_a" id="button_6" href="">Button 6</a></li>
        </ul>
    </li>
    </ul>
    </div>
    

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    水波映月 2024-12-08 14:47:12

    将列表项分配给变量,例如

    $(".block-buttons .level1 a.level1_a").click(function () 
    {
        var listItem = $(this).parent('li');
        $.getJSON('api/1/test.json', //etc
    

    Assign the list item to a variable, eg

    $(".block-buttons .level1 a.level1_a").click(function () 
    {
        var listItem = $(this).parent('li');
        $.getJSON('api/1/test.json', //etc
    
    不顾 2024-12-08 14:47:12

    最简单的方法是在您想要保留 this 的级别执行

    var that = this;

    - 然后稍后将其作为 that 访问。

    easiest way is to do

    var that = this;

    at the level you want to preserve this - and then access it as that later.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文