为什么我无法通过onclick属性调用名为clear的函数? [英] Why can't I call a function named clear from an onclick attribute?

查看:92
本文介绍了为什么我无法通过onclick属性调用名为clear的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的计算器,当单击一个按钮时,它的值显示在文本字段中,并且按钮C应该清除文本字段,但它的onclick =clear()不起作用? ?

 <%@ page contentType =text / htmlpageEncoding =UTF-8%> 
<!DOCTYPE html>
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>计算器< / title>
< style>
#button {
padding:10px;
}
< / style>
< script>
函数fill(val){
document.getElementById(field)。value + = val;
}
函数clear(){
document.getElementById(field)。value =;
}
< / script>
< / head>
< body>
< form>
< table>
< tr>< input type =textname =fieldid =fieldsize =8/>< / tr>
<%! int k = 1; %GT;
<%for(int i = 0; i< 3; i ++){%>
< tr> <%
for(int j = 0; j< 3; j ++){%>
< td>< input type =buttonid =buttononclick =fill(this.value)value =<%= k ++%> />< / TD>
<%}%>
< / tr>
<%}%>
< tr>

<! - 这里onclick =clear()不工作? - >

< td>< input type =buttonid =buttonvalue =Conclick =clear()/>< / td>
< td>< input type =buttonid =buttonvalue =0onclick =fill(this.value)< / td>
< td>< input type =submitid =buttonvalue ==< / td>
< / tr>
< / table>
< / form>
< / body>
< / html>


解决方案 onclick )是可怕的。在内部,他们使用

i'm trying to create a simple calculator, when a button is clicked its value is shown in the text field and the button "C" should clear the text field but its onclick="clear()" is not working??

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Calculator</title>
        <style>
            #button{
                padding: 10px;
            }
        </style>
        <script>
            function fill(val){
                document.getElementById("field").value+=val;
            }
            function clear(){
                document.getElementById("field").value="";
            }
        </script>
    </head>
    <body>
        <form>
        <table>
            <tr><input type="text" name="field" id="field" size="8"/></tr>
        <%! int k = 1; %>
        <% for(int i=0;i<3;i++){ %>
        <tr> <%
            for(int j=0;j<3;j++){ %>
            <td><input type="button" id="button" onclick="fill(this.value)" value="<%=k++%>" /></td>
          <%  } %>
        </tr>
        <% } %>
        <tr>

<!--here onclick="clear()" is not working?? -->

            <td><input type="button" id="button" value="C" onclick="clear()"/></td>
            <td><input type="button" id="button" value="0" onclick="fill(this.value)"</td>
            <td><input type="submit" id="button" value="="</td>
        </tr>
        </table>
        </form>
    </body>
</html>

解决方案

Intrinsic event attributes (like onclick) are horrible. Internally they implement with:

Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues.

Consequently, you are actually calling document.clear() instead of your global clear().

The quick fix for this is to rename the function to something else or explicitly call window.clear().

The better solution is to bind your event handlers with addEventListener instead of intrinsic event attributes.

这篇关于为什么我无法通过onclick属性调用名为clear的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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