jquery克隆多个实例为什么

发布于 2024-09-07 09:40:21 字数 1112 浏览 2 评论 0原文

希望可以提供帮助,尝试 Jquery 克隆,这似乎有效,但我在“压制”按钮时得到“多个”克隆而不是单个克隆。

例如:我想克隆这个:

echo '<select class="hello">';
    foreach ($pageposts as $post):
    echo '<option>'.$post->post_title.'</option>';
    endforeach;
    echo '</select>';

点击这个

echo '<input type="button" id="rp" value="add">';

是从WordPress和是的“hello”类来自JQuery页面

我的JQuery功能是:

$j=jQuery.noConflict();
$j(document).ready(function() {
$j('#rp').click(function(){ 
$j('.hello').clone().appendTo('#goodbye');
});
});

所以我的“整体代码片段”看起来像这样:

echo '<select class="hello">';
foreach ($pageposts as $post):
echo '<option>'.$post->post_title.'</option>';
endforeach;
echo '</select>';
echo '<div id="goodbye"></div>';
echo '<input type="button" id="rp" value="add">';

我在上克隆“一次”第一次按,但随后会出现多个,即:

1次点击给出1个克隆加1个原始版本 - 我想要的

2次点击给出3个克隆加1个原始版本 - 不是我想要的 我想要1个原始版本加2个

3次点击给出7个克隆加1个原始版本- 不是我想要的 我想要 1 原件加 3 等等,

请提出建议。谢谢

hope can help, trying out Jquery clone which seems to work, but I get "multiple" clones not single clones on "repressing" the button.

e.g.: I want to clone this :

echo '<select class="hello">';
    foreach ($pageposts as $post):
    echo '<option>'.$post->post_title.'</option>';
    endforeach;
    echo '</select>';

on click of this

echo '<input type="button" id="rp" value="add">';

Yes from WordPress and Yes the "hello" class is from the JQuery pages

My JQuery function is:

$j=jQuery.noConflict();
$j(document).ready(function() {
$j('#rp').click(function(){ 
$j('.hello').clone().appendTo('#goodbye');
});
});

So my "overall code snippit" looks like this:

echo '<select class="hello">';
foreach ($pageposts as $post):
echo '<option>'.$post->post_title.'</option>';
endforeach;
echo '</select>';
echo '<div id="goodbye"></div>';
echo '<input type="button" id="rp" value="add">';

I clone "once" on the first press, but then it goes in multiples i.e.:

1 click gives 1 clone plus 1 original - what I want

2 clicks gives 3 clones plus 1 original - not what I want I want 1 original plus 2

3 clicks gives 7 clones plus 1 original - not what I want I want 1 original plus 3
etc.

Suggestions please. Thanks

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

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

发布评论

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

评论(2

安静 2024-09-14 09:40:21

这是因为您的选择器正在寻找一个类:

$j('.hello')

每次您克隆并将其附加到另一个元素时,您都会添加另一个 .hello 元素,因此您会克隆每个 .hello它可以找到的元素。

也许您应该在克隆后删除类名:

$j('.hello').clone().removeClass('hello').appendTo('#goodbye');

或者甚至更改它:

$j('.hello').clone().removeClass('hello').addClass('cloned_hello').appendTo('#goodbye');

您可能想要添加不同的类,以便您的 CSS 仍然有效,但最终这就是您获得多个克隆项目的原因。

It's because your selector is looking for a class:

$j('.hello')

Everytime you clone and append this to another element you're adding yet another .hello element, therefore your cloning every single .hello element it can find.

Perhaps you should remove the class name when it's been cloned:

$j('.hello').clone().removeClass('hello').appendTo('#goodbye');

Or perhaps even change it:

$j('.hello').clone().removeClass('hello').addClass('cloned_hello').appendTo('#goodbye');

You might want to add a different class so your CSS will still work, but ultimately this is why you're getting multiple cloned items.

远昼 2024-09-14 09:40:21

该行为是正常的,因为您克隆了每个包含 hello 类的元素。

试试这个:

$j('.hello').clone().attr("class","cloned").appendTo('#goodbye');

然后你在 .cloned 中放入与 .hello 中相同的 css 。

希望它有帮助:)

the behaviour is normal, because you clone every element that has the hello class in it.

Try this :

$j('.hello').clone().attr("class","cloned").appendTo('#goodbye');

And you put in your css the same in .cloned as in .hello .

Hope it helps :)

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