jQuery getJSON:点击“this”从几个层次往下
摘要:尝试从多次调用中引用点击“this”。
大家好,
我有一种情况,我有一组无序列表形式的按钮(按钮 1 - 6)。单击其中一个按钮时,我希望它 getJSON 加载更多
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将列表项分配给变量,例如
Assign the list item to a variable, eg
最简单的方法是在您想要保留
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 asthat
later.