从具有相同类的元素克隆内容w/javascript
我对JavaScript知之甚少,我的任务是将一个DIV中的H2和p复制到另一个DIV中,并在悬停中显示。请看看我在这里想做什么: 我正在尝试从.orig克隆到.hover,它可以正常工作,但在每个工具提示中显示所有三个活动项,而不是单独显示 以下是我的列表项目:从具有相同类的元素克隆内容w/javascript,javascript,jquery,clone,code-duplication,Javascript,Jquery,Clone,Code Duplication,我对JavaScript知之甚少,我的任务是将一个DIV中的H2和p复制到另一个DIV中,并在悬停中显示。请看看我在这里想做什么: 我正在尝试从.orig克隆到.hover,它可以正常工作,但在每个工具提示中显示所有三个活动项,而不是单独显示 以下是我的列表项目: <ul> <li class="lactrimal1"> <div class="orig"> <h2>L
<ul>
<li class="lactrimal1">
<div class="orig">
<h2>Lactrimal Duct Probe</h2>
<p>Helps patient regain use of their tear ducts in this quick procedure.</p>
</div>
<div class="img">
<div class="txt hover"></div>
</div>
</li>
<li class="cataracts2">
<div class="orig">
<h2>Cataracts</h2>
<p>We replace the eye's natural lens which has become clouded by cataract with an artificial lens.</p>
</div>
<div class="img">
<div class="txt hover"></div>
</div>
</li>
<li class="sinus3">
<div class="orig">
<h2>Endoscopic Sinus Surgery</h2>
<p>A procedure used to remove blockages in the sinuses to allow for efficient pain-free breathing.</p>
</div>
<div class="img">
<div class="txt hover"></div>
</div>
</li>
</ul>
我也尝试过,但它只克隆了第一项:
$(".hover").html($('.orig').html());
感谢您的帮助,谢谢大家
你所有的答案都有效,我不知道这个问题有这么多解决方案。非常感谢你的帮助试试这样的方法 编辑: 实际上,你必须这样做才能到达元素
$('div.orig').each(function() {
var jThis = $(this);
jThis.parent().find('.hover').html(jThis.html());
});
anpother解决方案->
$(document).ready(function(){
$('div.orig', this).each(function(i,e) {
$(this).clone().appendTo('div.hover:eq(' + i + ')');
});
})
谢谢阿迪内奥!那个小提琴网站也很棒
$('div.orig').each(function() {
$(this).parent().find('.txt').html( $(this).html() );
});
$('.orig', this).each(function() {
$(this).next('.img').children('.hover').html($(this).html());
});
$(document).ready(function(){
$('div.orig', this).each(function(i,e) {
$(this).clone().appendTo('div.hover:eq(' + i + ')');
});
})