0

我有一些功能(例如)查找所有 div 并附加到它们。

function test() {
    $('div').append(/*...*/);
}

现在我正在通过 $.get 函数将新的 div 加载到我的容器元素中。加载新的 div 后,我想在 contex(仅限于)我的容器元素中调用 test() 函数。我只想将某物附加到新的 div 中。我不想将两次附加到旧 div 中。

如果可能的话,我不想编辑测试功能。

4

2 回答 2

1
function test(container) {
    $(container).find('div').append(/*...*/);
}

像这样使用:

test("body");
test("#mainContainer");
test("ul.spiffy > li");

简而言之,您只需将要修改的 div 的选择器传入其中。

如果您想允许“默认”值,您可以执行以下操作:

function test(container) {
    if (container == undefined) container = "body";
    $(container).find('div').append(/*...*/);
}

现在,如果您不传递任何参数进行测试,它将append应用于div.body

于 2012-09-11T22:12:47.740 回答
0
$('div', 'container').append(/* code */);
于 2012-09-11T22:07:42.520 回答