jQuery getJSON:点击';这';从几层下来
摘要:尝试引用从几个调用中单击“this” 大家好 我有一种情况,我有一组按钮(按钮1-6),以无序列表的形式。当点击其中一个按钮时,我希望它在点击的按钮下面加载更多的jQuery getJSON:点击';这';从几层下来,jquery,click,nested,this,getjson,Jquery,Click,Nested,This,Getjson,摘要:尝试引用从几个调用中单击“this” 大家好 我有一种情况,我有一组按钮(按钮1-6),以无序列表的形式。当点击其中一个按钮时,我希望它在点击的按钮下面加载更多的选项(基于json数据)。现在,当我单击其中一个按钮时,它会在所有按钮下加载基于JSON的选项。问题是,我不确定如何引用“click”函数调用的“this”,而不是与其中一个嵌套调用关联的“this” 谈到jQuery,我是一个大人物,所以我觉得这可能是一个简单的解决方案。如果我的术语有点不恰当,请原谅 感谢您阅读并考虑我的问题
$(".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;
});
$(“.block buttons.level1 a.level1_a”)。单击(函数()
{
$.getJSON('api/1/test.json',
函数(数据){
ul=$(“”);
$。每个(数据项,
函数(intIndex,objValue)
{
a=$(”
将列表项分配给变量,例如
$(".block-buttons .level1 a.level1_a").click(function ()
{
var listItem = $(this).parent('li');
$.getJSON('api/1/test.json', //etc
最简单的方法是
var that=this;
在您希望保留的this
-级别,然后作为that
访问它
$(".block-buttons .level1 a.level1_a").click(function ()
{
var listItem = $(this).parent('li');
$.getJSON('api/1/test.json', //etc