问题描述
我有一个链接.当有人点击它时,我想在让它工作之前检查一些条件.如果它是 false,则应该阻止默认操作.
I have a link. When some one clicks on that I want to check some conditions before letting it work. If it's false the default action should be prevented.
$(".pager-next a.active").click(function(event) {
if (!a == 1) {
event.preventDefault();
}
});
只有当 a 等于 1 时,该链接才有效.上面的代码是否正确.如果满足特定条件,则 a 设置为 1.该链接只有在满足条件时才有效.
The link should only work if a is equal to 1. Is the above code correct. a is set to 1 if a particular condition is met. The link should only work if the condition is met.
推荐答案
假设 'should only work if a is equal to 1' 你的意思是 a 元素等于 1,试试这个:
Assuming by 'should only work if a is equal to 1' you mean the text of the a element is equal to 1, try this:
$(".pager-next a.active").click(function(event) {
if ($(this).text() != "1") {
event.preventDefault();
}
});
您可以修改 text() 以使用 jQuery 中可用的元素属性.
You can amend text() to use whichever attribute of the element is available to you in jQuery.
更新
my a 是一个 var,在满足条件之前保持值为 0.
my a is a var which hold the value 0 until a condition is met.
在这种情况下,问题只是你的相等运算符不正确:
In which case, the problem was simply that your equality operator was incorrect:
$(".pager-next a.active").click(function(event) {
if (a != 1) {
event.preventDefault();
}
});
这篇关于如果条件为假,则阻止默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)