HTML5输入属性中的JavaScript动作? [英] JavaScript action inside HTML5 oninput attribute?

查看:126
本文介绍了HTML5输入属性中的JavaScript动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习HTML5。我遇到的一个例子使用了一个类型为range的输入元素和一个输出元素(这个例子目前只适用于Chrome,Safari和Opera)。

I have been learning HTML5. One of the examples I have encountered uses an input element of type range and an output element (this example currently only works in Chrome, Safari and Opera). The following form produces a slider with the result echoed to the output element.

<form>
   <p>
      <input type="range" id="slideValue" value="50" 
          oninput="slideCurrent.value = parseInt (slideValue.value);" />
      <output id="slideCurrent">50</output>
   </p>
   <input type="submit" value="Send">
</form>

我的问题涉及oninput属性。 oninput属性包含JavaScript。在pre-HTML5 JavaScript中,我通常会看到对 this.value 的JavaScript引用。但是,在上面的HTML5示例中,对 slideCurrent slideValue 的引用可以工作(显然不需要使用 getElementById )) 。我相信这是一种新的JavaScript行为方式。

My question concerns the oninput attribute. The oninput attribute contains JavaScript. In pre-HTML5 JavaScript I commonly see JavaScript references to this.value. However in the above HTML5 example the references to slideCurrent and slideValue work (apparently without the need to use getElementById). I believe this is a new way for JavaScript to behave.

这个新的JavaScript操作方法是否记录在某处?

Is this new JavaScript method of action documented somewhere?

推荐答案

内联事件处理程序中的代码作用于元素,就好像它在中带有块。

因此,您可以使用元素的属性作为全局变量。

Code within inline event handlers is scoped to the element, as if it was in a with block.
Therefore, you can use properties of the element as global variables.

这是一个鲜为人知的危险功能,并不是HTML5的新功能。

This is a little-known and dangerous feature, and is not new to HTML5.

这篇关于HTML5输入属性中的JavaScript动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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