2017-08-30 49 views
1

想要联系并询问是否有人有任何设计Chrome扩展的经验。我看到我的朋友在闲聊中谈论如果有一个阻止mailto的插件:如果直接链接到您的邮件应用程序,而是让您选择复制链接中的电子邮件或继续mailto:功能。jQuery Tooltip on .click,而不是悬停在Chrome扩展上

到目前为止,我继续找到一种方法,只针对该页面上的mailto:链接,阻止其默认操作,然后卡住了。我想知道是否有办法通过单击函数而不是悬停来启动工具提示,并动态地将元素插入到mailto:函数中,以使用上面描述的两个用户操作来打开工具提示窗口。

有人有什么想法吗?我完全是出于他们的,真的不知道如何着手:(

https://codepen.io/matthewvolk/pen/brOEgz

// Select ONLY mailto elements on DOM 
 
$('a[href^="mailto:"]').click(function (event) { 
 

 
    // Prevent default mailto: action 
 
    event.preventDefault(); 
 

 
    // Open tooltip containing mailto/copy option 
 
    $('a[href^="mailto:"]').tooltip({ 
 

 
     // This is where I'm stuck 
 

 
    }); 
 

 
});
<a href="mailto:[email protected]?Subject=Hello%20again" target="_top" title="test">MailTo Action</a> 
 
<br /><br /> 
 
<a href="https://google.com/" target="_blank">Link that still works</a> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
你可以做

回答

0

好一件事是通过每个邮寄地址链接循环在页面加载时并在你的CSS中添加你的工具提示,但默认情况下隐藏在你的CSS中。

然后,在点击时,切换工具提示的可见性

下面是一个示例,其中工具提示仅包含mailto链接,但您当然可以在要包含在工具提示中的.each()函数中添加其他标记。

在工具提示创建过程中,我们使用$(this).attr('href')$(this).text()填写工具提示内的链接。 ($(this)指的是每个链接的href以“mailto:”开头)。

然后,我们将原始链接和我们刚创建的工具提示封装在一个包装中,这使我们可以轻松地将工具提示放在样式表中。

$(function(){ 
 
    var mailtoLinks = $('a[href^="mailto:"]').each(function(){ 
 
    $(this).before(
 
     '<div class="tooltip">' + 
 
     '<a href="' + $(this).attr('href') + '">' + 
 
     $(this).text() + '</a>' + 
 
     '</div>' 
 
    ); 
 
    $(this).add($(this).prev('.tooltip')).wrapAll('<div class="tooltip-wrapper"></div>'); 
 
    }); 
 
    
 
    // Select ONLY mailto elements on DOM 
 
    $(mailtoLinks).click(function(event) { 
 
    // Prevent default mailto: action 
 
    event.preventDefault(); 
 
    $(this).prev('.tooltip').toggle(); 
 
    }); 
 
});
.tooltip-wrapper { 
 
    position: relative; 
 
} 
 
.tooltip { 
 
    position: absolute; 
 
    z-index: 1; 
 
    top: calc(-100% - 15px); 
 
    left: -5px; 
 
    padding: 5px; 
 
    background-color: lightgray; 
 
    display: none; 
 
} 
 
.tooltip::after { 
 
    content: ''; 
 
    border-top: 10px solid lightgray; 
 
    border-right: 10px solid transparent; 
 
    border-left: 10px solid transparent; 
 
    position: absolute; 
 
    bottom: -10px; 
 
    left: calc(50% - 7.5px); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div style="margin-left: 15%; margin-top: 15%"> 
 
<a href="mailto:[email protected]?Subject=Hello%20again" target="_top">MailTo Action</a> 
 

 
<br /> 
 
<br /> 
 
<br /> 
 

 
<a href="https://google.com/" target="_blank">Link that still works</a> 
 
</div>

注意,谷歌“链接仍然工作”似乎并不在这个片段的工作,但我认为这是一个堆栈溢出行为,而不是与此相关的代码。

+0

这太棒了!我绝对不会想到采取这种方法。也许我需要继续学习;)我仍然是一个初学者大声笑。 快速问题,想知道是否有方法来操纵工具提示中的文本是不同的? 如果有一种方法可以使用jQuery来复制HTML元素中的文本?我会开始使用Google搜索。十分感谢你的帮助!!!! – matthewvolk

+0

@matthewvolk乐于帮助。是的,当然你可以改变工具提示中的文字。在脚本中。您会看到我们正在创建一个''标签,然后在关闭的''标签前插入'$(this).text()'。但是,除了'$(this).text()'之外,你可以在其中添加任何其他内容。您可以使用'.text()'方法获取页面上任何元素的文本。你只需要做'$(。some-selector).text()'。如果你想为所有工具提示使用相同的文本,最好将它缓存在一个变量中,然后在'.each()'函数中使用该变量。 api.jquery.com有你需要知道的一切! – cjl750

+0

你是最棒的。就像我说的,我仍然在学习,得到这种建议非常有帮助。非常感谢cjl !!!!!!! – matthewvolk