简体   繁体   English

将链接克隆并附加到容器,但附加整个URL —而不是链接文本

[英]Clone & Append Link to Container, but Append the entire URL — Not the Link Text

This should be easy but I can't find any topics about this, I think it's because I don't know how to phrase it correctly. 这应该很容易,但是我找不到关于此的任何主题,我认为这是因为我不知道如何正确地表达它。

Problem: 问题:

When you hover over a link it gets cloned & appended to #container — however I want the link in #container to be the actual URL in its entirety, so if a link text says: "here is an article" , I want the one that gets appended to #container to show: http:/www.myarticle.com/name-of-article . 当您将鼠标悬停在链接上时,它将被克隆并附加到#container中-但是我希望#container中的链接完整地是实际的URL,因此,如果链接文本显示为: “这是一篇文章” ,我希望该链接附加到#container上以显示: http:/www.myarticle.com/name-of-article

I've tried to make my question very visually clear on CodePen, would someone check it out and advice me? 我试图在CodePen上使问题清晰可见,有人可以检查一下并为我提供建议吗? :-) :-)

http://codepen.io/StrengthandFreedom/pen/YqNrYO http://codepen.io/StrengthandFreedom/pen/YqNrYO

The jQuery I use: 我使用的jQuery:

$('a').one('mouseover', function(){

 $(this).clone().appendTo('#container'); 
});

Either JavaScript or jQuery solutions are fine, I use both. JavaScript或jQuery解决方案都可以,我都可以使用。

You need to update the link text to the value of the href attribute. 您需要将链接文本更新为href属性的值。

$('a').one('mouseover', function(){
 var href = $(this).attr('href'); 
 $(this).clone().text(href).appendTo('#container');
});

http://codepen.io/anon/pen/eZgeNe http://codepen.io/anon/pen/eZgeNe

You may use this: 您可以使用此:

$('a').one('mouseover', function(){
     $('#container').append($(this).attr('href')); 
});

You need to catch the attribute of the link, which is done by using attr('attribute-handle') 您需要捕获链接的属性,这可以通过使用attr('attribute-handle')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM