admin管理员组文章数量:1394971
I have a div what acts like a button(.butt), when I click it, It should prevent a href from redirecting and open a input. It dosent work, it still redirects and when I go back from redirection the input appears, but when I press the input it still redirects.
How can I make it so that when I press my div it disables the redirection and lets my insert a value into input to change the value?
var x;
var h = 0; // blur fires multiple times, use h to count and fire once
var url = 'up.php';
$(".butt").on('click', function(event) {
event.preventDefault();
$(".tk").on('click', function(d) {
d.stopPropagation();
var x = $(d.target);
x.html('<input id="edit2" style="width: 40px;" value="'+x.text()+'">');
var this_id = x.context.id;
$("#edit2").on('blur', function(d) {
if (h < 1) {
h = h+1;
d.stopPropagation();
$(d.target.parentElement).text(d.target.value);
$.get(url, {id: this_id, value: d.target.value})
.done(function(d){ if(d == undefined || d != 1) { alert('Something went wrong, check your data and results. '+d);}})
.fail(function(d){ alert('error');});
$("#edit2").off('blur');
}
});
h = 0;
});
});
.butt{width:15px;height:15px; background-color:red;border-radius: 50%;}
<script src=".1.1/jquery.min.js"></script>
<div class="butt"> </div>
<a href="" class="tk">click</a>
I have a div what acts like a button(.butt), when I click it, It should prevent a href from redirecting and open a input. It dosent work, it still redirects and when I go back from redirection the input appears, but when I press the input it still redirects.
How can I make it so that when I press my div it disables the redirection and lets my insert a value into input to change the value?
var x;
var h = 0; // blur fires multiple times, use h to count and fire once
var url = 'up.php';
$(".butt").on('click', function(event) {
event.preventDefault();
$(".tk").on('click', function(d) {
d.stopPropagation();
var x = $(d.target);
x.html('<input id="edit2" style="width: 40px;" value="'+x.text()+'">');
var this_id = x.context.id;
$("#edit2").on('blur', function(d) {
if (h < 1) {
h = h+1;
d.stopPropagation();
$(d.target.parentElement).text(d.target.value);
$.get(url, {id: this_id, value: d.target.value})
.done(function(d){ if(d == undefined || d != 1) { alert('Something went wrong, check your data and results. '+d);}})
.fail(function(d){ alert('error');});
$("#edit2").off('blur');
}
});
h = 0;
});
});
.butt{width:15px;height:15px; background-color:red;border-radius: 50%;}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="butt"> </div>
<a href="http://www.google." class="tk">click</a>
Share
Improve this question
edited Feb 10, 2017 at 11:22
Somepub
asked Feb 10, 2017 at 11:15
SomepubSomepub
4352 gold badges7 silver badges23 bronze badges
1 Answer
Reset to default 5You can use preventDefault
The event.preventDefault() method stops the default action of an element from happening.
d.preventDefault();
var x;
var h = 0; // blur fires multiple times, use h to count and fire once
var url = 'up.php';
$(".butt").on('click', function(d) {
$(this).on("click", function(){return false;})
$(".tk").on('click', function(d) {
d.preventDefault();
d.stopPropagation();
var x = $(d.target);
x.html('<input id="edit2" style="width: 40px;" value="'+x.text()+'">');
var this_id = x.context.id;
$("#edit2").on('blur', function(d) {
if (h < 1) {
h = h+1;
d.stopPropagation();
$(d.target.parentElement).text(d.target.value);
$.get(url, {id: this_id, value: d.target.value})
.done(function(d){ if(d == undefined || d != 1) { alert('Something went wrong, check your data and results. '+d);}})
.fail(function(d){ alert('error');});
$("#edit2").off('blur');
}
});
h = 0;
});
});
.butt{width:15px;height:15px; background-color:red;border-radius: 50%;}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="butt"> </div>
<a href="http://www.google." class="tk">click</a>
本文标签: javascriptHow can I prevent href redirect onclickStack Overflow
版权声明:本文标题:javascript - How can I prevent href redirect onclick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744107227a2591121.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论