admin管理员组文章数量:1332377
HTML:
<a href="#" class="button" onclick="sendDetails(\'Edu\')">
JS:
function sendDetails(type) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
names = $("#names" + type).val();
Input = encodeURIComponent($("#Input" + type).val());
....
The link jumps to top of page. I tried to use event.preventDefault()
to stop jumping to top of page. However, it works only in Chrome and not in IE and Firefox. How can I solve it?
HTML:
<a href="#" class="button" onclick="sendDetails(\'Edu\')">
JS:
function sendDetails(type) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
names = $("#names" + type).val();
Input = encodeURIComponent($("#Input" + type).val());
....
The link jumps to top of page. I tried to use event.preventDefault()
to stop jumping to top of page. However, it works only in Chrome and not in IE and Firefox. How can I solve it?
-
5
How do you get the
event
withinsendDetails(type)
? – thecodeparadox Commented Feb 18, 2013 at 18:10 - try to add an return false at the end of the function ... – tobspr Commented Feb 18, 2013 at 18:11
-
and from where are you calling
sendDetails
? – adeneo Commented Feb 18, 2013 at 18:11 -
1
Where does
event
e from? How are you callingsendDetails
? jQuery provides a unified event interface, you can safely callevent.preventDefault
. – Felix Kling Commented Feb 18, 2013 at 18:12 - <a href="#" class="button" onclick="sendDetails(\'Edu\')">..this is how I calling in my php page... – user1854007 Commented Feb 18, 2013 at 18:13
3 Answers
Reset to default 3instead of "#" you can use javascript:;
so there is no jumping, make sure to return false to disable the link-behavior
<a href="javascript:;" onclick="something();return false;">link</a>
You can't only use the window.event
to control an event. Try standardizing it like:
function sendDetails(e, type) {
var evt = window.event || e;
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
// ...
}
And your HTML would have to be:
<a href="#" class="button" onclick="sendDetails(event, 'Edu');">ASDF</a>
One other non-jQuery solution is to just modify your HTML to be:
<a href="#" class="button" onclick="sendDetails(event, 'Edu'); return false;">ASDF</a>
in which case you wouldn't have to use anything dealing with event
in your sendDetails
function. The return false;
will prevent the default behavior automatically. But note - if any exceptions occur in your sendDetails
function, the return false;
won't execute and will allow the default behavior. That's why I like using preventDefault
- you can call it immediately in the function to immediately stop the behavior, then do what you need.
At the same time, if you're using jQuery, try binding the click event like this:
$(document).ready(function () {
$(".button").on("click", function (e) {
e.preventDefault();
// Your sendDetails code (without the "event" stuff)
// OR call sendDetails (and remove the "event" stuff in the sendDetails function)
});
});
in which case your HTML would be:
<a href="#" class="button">ASDF</a>
Although it would be a lot easier to target the specific elements that this applies to, instead of using the .button
selector I provided. I'm sure the "button" class applies to more than just these targeted <a>
, but maybe I'm wrong :)
Using jQuery is nice in this situation because it already standardizes the event
object in a way that you can just use that e
variable I included in the click
callback. I'm sure it does a little more than just window.event || e
, so I'd prefer/suggest using jQuery for handling events.
You are already using jQuery, just do it the jQuery way. jQuery wraps the event object and provides a normalized event object so you can just use the standard preventDefault, you don't need to fork depending on what the browser supports.
<button class="senddetail" data-type="edu">Education</button>
<button class="senddetail" data-type="">Commercial</button>
<!-- why not just use a button instead of styling a link to look
like a button? If it does need to be a link for some reason
just change this back to an anchor tag, but keep the data
attributes and change the class to "button senddetail" -->
<script>
function sendDetails(type) {
// Assuming names and input are not globals you need to declare
// them or they will bee implicit globals which can cause
// all sorts of strange errors if other code uses them too
var names, input;
names = $("#names" + type).val();
// you should only use capitalized variables for
// Constructor functions, it's a convention in JS
input = encodeURIComponent($("#Input" + type).val());
//....
}
// just calling $ with a function inside of the invocation
// is the same as using $(document).ready
$(function () {
// instead of using onClick, use jQuery to attach the
// click event in a less intrusive way
$('.senddetail').on('click', function (event) {
// no matter what browser this runs in jQuery will
// provide us a standard .preventDefault to use
event.preventDefault();
// jQuery sets 'this' to the DOM element that the event fired on,
// wrapping it in another jQuery object will allow us to use the
// .data method to grab the HMLM5 data attribute
var type = $(this).data('type');
sendDetails(type);
});
});
</script>
本文标签:
版权声明:本文标题:javascript - event.preventDefault() in a href="#" doesn't prevent jumping to top of page in IE and Fir 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742305305a2449795.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论