如何防止<a>
第一次单击时的默认操作(以便显示工具提示),然后在第二次单击时将用户定向到href
元素上设置的属性值?
的HTML
<a id="second" href="https://www.test.com/" title="" class="customtooltip c_tool2" data-original-title="data del toolltip numero 2">tooltip</a>
jQuery的
var t = $('.c_tool2'), b = $('a[href^="http');
b.click(function(e){
if(t.length > 0){
e.preventDefault();
t.tooltip('show');
} else {
// on second click direct user to value of href
}
});
假设将有多个元素,则无法使用公共计数器。您可以使用data-*
属性对单个元素使用计数器
此外,用于选择锚点的选择器不正确。
var t = $('.c_tool2'),
b = $('a[href^="http"]'); // <-- Added "] to make it correct
b.click(function (e) {
// Get the counter for clicked element
var clickCount = parseInt($(this).data('count'), 10) || 0;
// If counter is zero i.e. for the first click
if (!clickCount) {
// Update the value of the counter
$(this).data('count', ++clickCount);
t.tooltip('show');
// Prevent the default action of the page redirection
e.preventDefault();
}
// Else, follow the default action
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句