[英]How to dissable href and call onclick at once with(out) jquery?
I found some answers for this question, but non of them worked for me. 我找到了这个问题的一些答案,但没有一个对我有用。
What I wants is to call function myFunc()
without refreshing page where href
wants. 我想要的是调用函数
myFunc()
而不刷新href
要的页面。
This href
is doing his job with GET
request and I want it to stay in cause if someone have script block. 这个
href
正在执行GET
请求,如果有人有脚本块,我希望它保持原状。 It have to work without scripts too. 它也必须在没有脚本的情况下工作。
But when somebody hadn't dissabled script, then I wants to not go where href
redirect but do myFunc()
onclick 但是,当有人没有禁用脚本时,我不想走到
href
重定向的地方,而是在onclick上执行myFunc()
I had Tried something like this: 我试过这样的事情:
at javascript: 在JavaScript:
function onClickFunc(somedata){
event.preventDefault();
myFunc(somedata);
}
at html: 在html上:
<a onclick="javascript:onClickFunc({{ some_django_data.id }})" href="?current={{ some_django_data.id }}{% if other_django_data %}&page={{ other_django_data.number }}{% endif %}">
When I replace href
body with #
and put javascript:myFunc({{ some_django_data.id }})
inside onclick
then it works. 当我用
#
替换href
主体并将javascript:myFunc({{ some_django_data.id }})
放在onclick
内时,它将起作用。
Bit it's not soloution because people with script block would lose possibility of using my site. 有点遗憾,因为具有脚本块的人会失去使用我的网站的可能性。
What is wrong with this solution? 该解决方案有什么问题?
Two things: 两件事情:
onXyz
attribute-style handlers contain JavaScript code , so there's no need for the javascript:
pseudo-protocol. onXyz
属性样式的处理程序包含JavaScript代码 ,因此不需要javascript:
伪协议。
Because you're coding a function call, if you want access to the event object, you need to pass it into the function 由于您正在编写函数调用的代码,因此,如果要访问事件对象,则需要将其传递给函数
So: 所以:
onclick="onClickFunc(event, {{ some_django_data.id }})"
Then 然后
function onClickFunc(event, somedata) {
event.preventDefault(); // Stops the default behavior of following the link
// ...
}
If you need to support really old browsers (or browsers hobbling themselves and behaving like really old browsers [I'm looking at you, IE9-IE11]), you'll need to allow for the possibility that preventDefault
isn't present on the event object. 如果您需要支持真正的旧版浏览器(或像真正的旧版浏览器一样跳动起来并表现为浏览器的浏览器,[我正在看着您,IE9-IE11]),则需要考虑使
preventDefault
不存在于浏览器中的可能性。事件对象。 The good news is, though, that return false
in an onXyz
attribute handler prevents the default action. 好消息是,在
onXyz
属性处理程序中return false
阻止默认操作。 So: 所以:
So (note the return
) : 因此(注意
return
) :
onclick="return onClickFunc(event, {{ some_django_data.id }})"
and (note the return
here too) : 和(也请注意这里的
return
) :
function onClickFunc(event, somedata) {
if (event.preventDefault) {
event.preventDefault(); // Stops the default behavior of following the link
}
// ...
return false;
}
Your code is on the right track, but you forgot to pass the event object to the javascript function. 您的代码在正确的轨道上,但是您忘记了将事件对象传递给javascript函数。
SEE THIS WORKING EXAMPLE: 查看此工作示例:
function myfunc(event, data) { event.preventDefault(); alert("HELLO"); return false; }
<a onclick="myfunc(event, 'foo')" href="www.google.com">TESTER</a>
Note: You do not really need to specify the JS protocol. 注意:您实际上不需要指定JS协议。 Have a look at this SO post .
看看这个SO帖子 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.