onClick =" javascript:function('value')'之间的区别是什么?和onClick =“function('value');”? [英] What is the difference between onClick="javascript: function('value')'" and onClick="function('value');"?

查看:107
本文介绍了onClick =" javascript:function('value')'之间的区别是什么?和onClick =“function('value');”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下几点有何区别?


  1. onClick =javascript:function('value') ;

  2. onClick =function('value');

何时在函数调用之前使用 javascript:,为什么?



我可以在不使用 javascript:前缀的情况下调用函数吗?



请在元素的内联事件处理程序中,如 onclick $ c>, onchange onsubmit 等等,你做 not 需要 javascript: prefix - 你经常看到它的原因是因为人们将它与下面解释的 href 语法混淆。它不会导致错误 - 我相信它被解释为标签 - 但它是没有必要的。

无论您是要调用一个函数还是运行一个简单的JS语句,都无关紧要'包括 javascript: - 也就是说,所有这三个都是有效的:

  onclick =doSomething('some val'); 
onclick =返回false;
onclick =doSomething(); doSomethingElse(); return false;

如果使用内联事件属性,则不要使用 javascript:前缀



(我说如果使用内嵌事件属性这种做法实在太过时了:最好在脚本块中分配使用JS的事件处理程序。)



您只需要 javascript:前缀,当您想从< a> 元素的 href 属性中运行JavaScript时, :

 < a href =javascript:someFunc();>无论< / a> 

这是因为浏览器通常需要 href 包含一个URI,所以 javascript:前缀告诉它预期JS代码。不过,我不建议这样做,因为该页面不适用于禁用JS的人员。最好包含一个 href ,它引导用户访问一个页面,告诉他们启用JS,并包含一个 onclick 来完成JS功能:

 < a href =/ enableJS.htmlonclick =doSomething(); return false; >无论< / A> 

通过这种方式,链接对用户有用,无论他们是否启用了JS。点击处理程序末尾的返回false 可防止点击的默认行为(这将导航到指定的URL)。


What is the difference between the following?

  1. onClick="javascript: function('value');"
  2. onClick="function('value');"

When do I use the javascript: before the function call, and why?

Can I call the function without using javascript: prefix?

Please help me understand the difference.

解决方案

In an element's inline event handler, like onclick, onchange, onsubmit, etc., you do not need the javascript: prefix - the reason you often see it is because people confuse it with the href syntax that I explain below. It doesn't cause an error - I believe it is interpreted as a label - but it is unnecessary.

It doesn't matter whether you want to call a function or run a "simple" JS statement, either way don't include javascript: - that is, all three of these are valid:

onclick="doSomething('some val');"
onclick="return false;"
onclick="doSomething(); doSomethingElse(); return false;"

If you are using inline event attributes don't use the javascript: prefix.

(I say "if you are using inline event attributes" because this practice is really outdated: it is better to assign the event handlers using JS in a script block.)

You only need the javascript: prefix when you want to run JavaScript from an <a> element's href attribute like this:

<a href="javascript: someFunc();">Whatever</a>

That is because the browser normally expects the href to contain a URI, so the javascript: prefix tells it to expect JS code instead. However, I don't recommending doing that because the page won't work for people who have JS disabled. Better to include an href that directs the user to a page telling them to enable JS, and include an onclick to do the JS functionality:

<a href="/enableJS.html" onclick="doSomething(); return false;">Whatever</a>

That way the link does something useful for users whether they have JS enabled or not. The return false at the end of the click handler prevents the default behaviour for a click (which would be to navigate to the specified URL).

这篇关于onClick =&quot; javascript:function('value')'之间的区别是什么?和onClick =“function('value');”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆