我正在尝试通过javascript将mailto链接添加到工具提示中的元素。我必须将代码放在document.onclick
中,因为代码不会运行,如果不是的话。所以现在我有它工作,但问题是你需要在链接上点击两次才能触发它。有没有办法删除2次点击?如果此功能不在document.onclick中,则它不起作用。提前谢谢..
document.onclick = function(){
jQuery(".gm_popup .email").click(function(){
var text = jQuery(this).text();
document.location = "mailto:" + text.trim();
});
}
答案 0 :(得分:0)
不要在点击时将事件附加到您的元素,而是在页面加载时附加它。由于您已经在使用jquery:
$(function() {
//Anything you place in here won't be run until the page has loaded
});
所以,在你的情况下,做这样的事情
$(function() {
jQuery(".gm_popup .email").click(function(){
var text = jQuery(this).text();
document.location = "mailto:" + text.trim();
});
});
答案 1 :(得分:0)
不确定是否要单击元素,以便工具提示显示链接,然后在工具提示链接中第二次单击。 除此之外,您可以尝试:
$(document).ready(function() {
$(".gm_popup .email").click(function(event){
event.preventDefault();
var text = jQuery(this).text();
document.location = "mailto:" + text.trim();
});
});
希望有所帮助!
答案 2 :(得分:0)
似乎您希望在显示时将mailto事件处理程序添加到工具提示链接,因此在单击文档时将事件绑定到链接。在这种情况下,也许$(“。gm_popup .email”)。on(“click”,handler)是更好的选择。
答案 3 :(得分:0)
Nailed it
$(document).on('click', '.gm_popup .email', function(){
var text = jQuery(this).text();
document.location = "mailto:" + text.trim();
});