admin管理员组文章数量:1426975
I have a problem to disable my anchor when its being pressed.
HTML:
<a href='#' data-bind='click: clickActivateSpatialSearch' id='draw_polygon'>
<i class='polygon_mark'></i>
</a>
<a href='#' data-bind='click: clickActivateSpatialSearchBox' id='draw_box'>
<i class='square_mark'></i>
</a>
jQuery
$('.square_mark').click(function () {
if(!$(this).hasClass('active')) {
$(this).addClass('active');
$('.polygon_mark').off('click', disabler);
} else {
$(this).removeClass('active');
$('.polygon_mark').on('click', disabler);
}
});
$('.polygon_mark').click(function () {
if(!$(this).hasClass('active')) {
$(this).addClass('active');
$('.square_mark').off('click', disabler);
} else {
$(this).removeClass('active');
$('.square_mark').on('click', disabler);
}
});
});
Function:
disabler: function(event) {
event.preventDefault();
return false;
},
The functionality to change classes are working fine, but what I wanted to add into this, os when anchor #1 is being pressed, this disables the other anchor, and when I press it once again, I'd like it to reenable the anchor, and also the other way around but for anchor #2.
I tried doing something like this, but I cannot get it to work, not fully understanding it either.
I have a problem to disable my anchor when its being pressed.
HTML:
<a href='#' data-bind='click: clickActivateSpatialSearch' id='draw_polygon'>
<i class='polygon_mark'></i>
</a>
<a href='#' data-bind='click: clickActivateSpatialSearchBox' id='draw_box'>
<i class='square_mark'></i>
</a>
jQuery
$('.square_mark').click(function () {
if(!$(this).hasClass('active')) {
$(this).addClass('active');
$('.polygon_mark').off('click', disabler);
} else {
$(this).removeClass('active');
$('.polygon_mark').on('click', disabler);
}
});
$('.polygon_mark').click(function () {
if(!$(this).hasClass('active')) {
$(this).addClass('active');
$('.square_mark').off('click', disabler);
} else {
$(this).removeClass('active');
$('.square_mark').on('click', disabler);
}
});
});
Function:
disabler: function(event) {
event.preventDefault();
return false;
},
The functionality to change classes are working fine, but what I wanted to add into this, os when anchor #1 is being pressed, this disables the other anchor, and when I press it once again, I'd like it to reenable the anchor, and also the other way around but for anchor #2.
I tried doing something like this, but I cannot get it to work, not fully understanding it either.
Share Improve this question asked Mar 12, 2013 at 12:08 WeeklyDadWeeklyDad 3171 gold badge4 silver badges15 bronze badges 1- What exactly does not work? What buttons do you press and what happens not as expected? – Bergi Commented Mar 12, 2013 at 12:12
2 Answers
Reset to default 3Do this and add a disable class to your css.
$('.square_mark').click(function () {
$(this).toggleClass('active');
$('.polygon_mark').toggleClass("disabled");
});
$('.polygon_mark').click(function () {
$(this).toggleClass('active');
$('.square_mark').toggleClass("disabled");
});
this should do the tric.
.disabled {
pointer-events: none;
cursor: default;
}
Edit: example on jsfiddle: http://jsfiddle/uKCYN/
And if you need to check for the active class to do something else you can code it like this:
$('.square_mark').click(function () {
if(!$(this).hasClass('active'))
$(this).addClass('active');
else
$(this).removeClass('active');
$('.polygon_mark').toggleClass("disabled");
});
$('.polygon_mark').click(function () {
if(!$(this).hasClass('active'))
$(this).addClass('active');
else
$(this).removeClass('active');
$('.square_mark').toggleClass("disabled");
});
and add your additional stuff into the if clauses.
Just modify the handler function like this and try again:
// handler function
function disabler(event) {
event.preventDefault();
console.log('item anchor clicked');
}
本文标签: javascriptDisable anchor with onclickStack Overflow
版权声明:本文标题:javascript - Disable anchor with onclick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745468613a2659643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论