jquery克隆多个实例为什么
希望可以提供帮助,尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您的选择器正在寻找一个类:
每次您克隆并将其附加到另一个元素时,您都会添加另一个
.hello
元素,因此您会克隆每个.hello
它可以找到的元素。也许您应该在克隆后删除类名:
或者甚至更改它:
您可能想要添加不同的类,以便您的 CSS 仍然有效,但最终这就是您获得多个克隆项目的原因。
It's because your selector is looking for a class:
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:
Or perhaps even change it:
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.
该行为是正常的,因为您克隆了每个包含 hello 类的元素。
试试这个:
然后你在 .cloned 中放入与 .hello 中相同的 css 。
希望它有帮助:)
the behaviour is normal, because you clone every element that has the hello class in it.
Try this :
And you put in your css the same in .cloned as in .hello .
Hope it helps :)