jQuery DIV点击,带锚点 [英] jQuery DIV click, with anchors
问题描述
为了制作可点击的div,我这样做:
To make click-able divs, I do:
<div class="clickable" url="http://google.com">
blah blah
</div>
然后
$("div.clickable").click(
function()
{
window.location = $(this).attr("url");
});
我不知道这是不是最好的方式,但它与我完美配合,除了一个问题:
如果div包含一个可点击的元素,例如
< a href =...>,并且用户点击超链接,超链接和div-可点击被称为
I don't know if this is the best way, but it works perfectly with me, except for one issue: If the div contains a click-able element, such as <a href="...">, and the user clicks on the hyperlink, both the hyperlink and div's-clickable are called
当锚标记引用javascript AJAX函数时,这是一个特别的问题,它执行AJAX函数 AND 跟随在div的'url'属性中链接。
This is especially a problem when the anchor tag is referring to a javascript AJAX function, which executes the AJAX function AND follows the link in the 'url' attribute of the div.
无论如何?
推荐答案
如果从函数返回false,它将停止事件冒泡,因此只会触发第一个事件处理程序(即你的锚点不会看到点击)。
If you return "false" from your function it'll stop the event bubbling, so only your first event handler will get triggered (ie. your anchor will not see the click).
$("div.clickable").click(
function()
{
window.location = $(this).attr("url");
return false;
});
参见 event.preventDefault()与return false 有关return false与preventDefault的详细信息。
See event.preventDefault() vs. return false for details on return false vs. preventDefault.
这篇关于jQuery DIV点击,带锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!