我有一个 jquery 函数和一个 javascript 函数。单击链接时,我想先运行 jquery 函数,如果返回 true,则它应该调用 javascript 函数。
html文件
<a href="javascript:void()" onclick="return $('#a').click() ? addInput(): true" >
J查询函数
$(function () {
$('a').click(function () {
var clickCount = jQuery.data(this, 'clickCount');
if (!clickCount) {
clickCount = 0
}
clickCount++;
jQuery.data(this, 'clickCount', clickCount);
alert(clickCount);
if (clickCount < 4) {
return true;
} else {
return false;
}
});
});
Javascript 函数
<script>
#.. do something
</script>
我的问题是 jquery 中的 if 条件不起作用。但是调用了 javascript 函数并且 javascript 函数工作正常。
我做错了吗?
我该怎么做
提前致谢
最佳答案
html文件
<a href="javascript:void()" >
J查询函数
<script type="text/javascript">
$(document).ready(function(){
$('a').click(function(event)
{
var clickCount = jQuery.data( this, 'clickCount');
if (!clickCount)
{
clickCount = 0
}
clickCount++;
jQuery.data( this, 'clickCount',clickCount);
if(clickCount<4)
{
//Do something
}
else
{
event.preventDefault();
}
});
关于javascript - 如果条件在 jquery 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17342933/