If conditions false then prevent default(如果条件为假,则阻止默认)
问题描述
我有一个链接.当有人点击它时,我想在让它工作之前检查一些条件.如果它是 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();
}
});
这篇关于如果条件为假,则阻止默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果条件为假,则阻止默认


基础教程推荐
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01