简体   繁体   English

脚本标签绑定方法-JavaScript

[英]Script Tag Binding Method - JavaScript

The following code is NOT working as expected. 以下代码无法正常工作。 I am using "Script Tag Binding Method" to have the events fired. 我正在使用“脚本标签绑定方法”来触发事件。 Can you please FIX this ? 您能解决这个问题吗? Thanks 谢谢

<html>

    <head>

                <title>
                    Example
                </title>

    </head>

    <body>

        <input type="button" name="cmdScriptTag" value="Script Tag">

                <script language="javascript" For="cmdScriptTag" Event="OnClick">

                    alert("Script Tag Binding Method.");

                </script>

        </body>

</html>

In your code you have: 在您的代码中,您具有:

> <script language="javascript" For="cmdScriptTag" Event="OnClick">

The language attribute for script elements was deprecated in HTML 4 and is removed in HTML5. 脚本元素的语言属性在HTML 4中已弃用,在HTML5中已删除。 The type attribute is required in HTML 4 and optional in HTML5. type属性在HTML 4中是必需的,在HTML5中是可选的。 In practice, it's not necessary, you can just use: 实际上,没有必要,您可以使用:

<script>
  /* content */
</script>

The for attribute for script elements is a Microsoft proprietary feature that is not part of any standard and probably not supported at all outside of IE (and possibly not supported by current IE versions). 脚本元素的for属性是Microsoft专有的功能,它不是任何标准的一部分,并且可能在IE之外完全不受支持(并且当前IE版本可能不支持)。 It was reserved for future use in the DOM 2 HTML spec but never implemented for script elements (as far as I know). 它保留供DOM 2 HTML规范将来使用,但据我所知从未用于脚本元素。 It applied only to label elements in HTML 4.01, same in HTML5 . 它仅适用于HTML 4.01中的标签元素 ,与HTML5中相同。

As far as I'm aware, there never was an event attribute, it may have been related to the for attribute but neither are listed at MSDN . 据我所知,从来没有一个 事件属性,它可能与 for属性有关,但都没有在 MSDN上列出。

The event attribute is similar to the for attribute: it's an IE feature that's not widely implemented and associates the script within a script element with an event for another element. event属性与for属性相似:它是一种IE功能,尚未广泛实施,并将脚本元素中的脚本与另一个元素的事件相关联。 Essentially it's the same as using other standards compliant methods to associate script with elements and events but a lot clunkier. 从本质上讲,这与使用其他符合标准的方法将脚本与元素和事件相关联是相同的,但是笨拙得多。

In any case, to associate a function with an element that will be called when the element receives a certain event, you can use a variety of methods. 在任何情况下,要将函数与将在元素接收到特定事件时调用的元素相关联,可以使用多种方法。 The most robust and widely implemented are inline listeners, eg: 内联侦听器是最可靠且实现最广泛的,例如:

<input type="button" name="cmdScriptTag" value="Script Tag"
 onclick="alert('Script Tag Binding Method.');">

Though generally the script body is put in a function that is called by the listener, eg 尽管通常将脚本主体放在侦听器调用的函数中,例如

<script>
  function foo(){
      alert('Script Tag Binding Method.');
  }
</script>
<input type="button" name="cmdScriptTag" value="Call foo"
 onclick="foo();">

You can also attach listeners dynamically using various methods such as addEventListener , which is explained on the Mozilla Developer Network (MDN). 您还可以使用各种方法动态附加侦听器,例如addEventListener ,这在Mozilla开发人员网络 (MDN)中进行了说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM