[英]PreventDefault Not Working
I am trying to use a preventDefault action on some hyperlinks to prevent them being activated, instead calling some ajax. 我正在尝试对某些超链接使用preventDefault操作,以防止它们被激活,而是调用一些Ajax。 However it doesn't seem to work.
但是,它似乎不起作用。
The code I am using is as follows: I have the following code: 我使用的代码如下:我有以下代码:
$("#" + container_id + " a[id^=read_more_link]").click(function(event){
alert($(this).html());
event.preventDefault();
});
Now what is interesting is the alert box fires so its definitely registering the click event, however it does not seem to recognise the preventDefault() call. 现在有趣的是,警报框将触发,因此它肯定注册了click事件,但是它似乎无法识别preventDefault()调用。
I tried a simple test simplifying my code and it still does not work: 我尝试了一个简单的测试来简化我的代码,但仍然无法正常工作:
<a id="read_more_link" data-article_id="1" href="news/1">Read More</a>
$("#read_more_link").on("click", function(event){
alert("test");
event.preventDefault();
});
Alert box fires but then page follows the link still. 警报框会触发,但是页面仍会跟随链接。
Note: It works If I use 'Return False' but not with preventDefault 注意:如果我使用“ Return False”但不使用preventDefault,则可以使用
Here you are! 这个给你!
function(e){
e = e || window.event;
e.preventDefault();
}
Fiddle: http://jsfiddle.net/rXCcB/2/ 小提琴: http : //jsfiddle.net/rXCcB/2/
Edit: As 11684 correctly pointed out, a detailed explanation would be helpful. 编辑:正如11684正确指出的那样,详细的说明会有所帮助。
Some browsers recognize e
as a window.event
, which contains all the information on an event; 一些浏览器将
e
识别为window.event
,其中包含有关事件的所有信息。 in this case, the click event you're trying to manipulate. 在这种情况下,就是您要操纵的click事件。 A few other browsers don't, so what happens at the first step in the function is a normalization across all browsers on the definition of
e
. 其他一些浏览器则没有,因此该函数的第一步是在所有浏览器上对
e
的定义进行标准化。 End result is that every reference to e
points to window.event
, at which point you can safely use e.preventDefault()
, knowing that all browsers understand exactly what you mean. 最终结果是,每个对
e
引用都指向window.event
,这时您可以安全地使用e.preventDefault()
,知道所有浏览器都完全理解您的意思。
It seems that you are preventing the default action of the link which has some parent, then your selector should be like this: 看来您正在阻止具有某些父项的链接的默认操作,然后选择器应如下所示:
my html is: 我的html是:
<p id='aaa'>
<a id='read_more_link' href='#'>adf</a>
</p>
the jquery: jQuery的:
var container_id = $('#aaa');
$("a[id^='read_more_link']", container_id).click(function(event) {
alert($(this).html());
event.preventDefault();
});
Checkout the fiddle: http://jsfiddle.net/EEZEe/ 查看小提琴: http : //jsfiddle.net/EEZEe/
see if this help you out. 看看这是否对您有帮助。
There is at least one easier, and one better way of doing it: 至少有一种更简单,更好的方法:
Easier: 更轻松:
$("#" + container_id + " a[id^=read_more_link]").click(function(){
alert($(this).html());
return false;
});
No need to use the event parameter, and no need to call a function. 无需使用event参数,也无需调用函数。
Better: 更好:
Use a button instead of a link: 使用按钮代替链接:
<p id='aaa'>
<button id='read_more_link' type='button'>adf</button>
</p>
Then you don't run into this problem, since a button is made for exactly this - events. 那么您就不会遇到这个问题,因为正是为此按钮(事件)。 Links are made for linking, and
return false
or preventDefault()
are, while they may work, workarounds. 建立链接以进行链接,
return false
或preventDefault()
是可行的解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.