【发布时间】:2016-07-03 11:50:57
【问题描述】:
- 当您将鼠标悬停在 任何 超链接(具有“.link”类)。
- 然后,当您单击附加的 (.link-cloner) 类时,我想克隆它附加到的超链接,并将该(链接)附加到#container。
我快到了,我只是无法完成最后一部分。
代码笔: http://codepen.io/StrengthandFreedom/pen/JXEEQP/
我尝试在各种组合中使用 find()、closest() 和 $(this),但我不能让它只克隆超链接(不是 linkCloner)并将其附加到 #container。
jQuery:
$(document).ready(function(e) {
/* ------------------------
Part 1 — WORKS
--------------------------*/
// Store link-cloner div in variable
var linkCloner = $('<div class="link-cloner">Cloner</div>');
// When mouse hover over any hyperlink
$('.link').on('mouseover', function() {
// Append the link-cloner class to the hyperlink
$(this).append(linkCloner);
}).mouseleave(function() {
//on mouse leave, remove link-cloner
$(linkCloner).remove();
});
/* ------------------------
Part 2 — DOESN'T WORK
--------------------------*/
//Then when you click on the appended linkCloner,
clone the hyperlink and append it to the #container
$(linkCloner).on('click', function() {
// This code is wrong....
event.preventDefault();
$('.link').clone().append('<li></li>').appendTo('#container');
});
});
有人可以引导我走向正确的方向吗? JavaScript 或 jQuery,我都可以(我都在学习):-)
【问题讨论】:
标签: javascript jquery html css