本文介绍了帮助Onclick Vb.Net-创建要单击的代码.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮我创建要单击的代码.

<输入类型=按钮" id ="ppp0"值=断开连接" name ="wanAct" onclick ="wanConnectAct(" ppp0,``disconnect")">

VB.NET

Help me create code to click.

<input type = "button" id = "ppp0" value = "Disconnect" name = "wanAct" onclick = "wanConnectAct (''ppp0'', ''disconnect'')">

VB.NET

推荐答案

<input type = "button" id = "ppp0" value = "Disconnect" name = "wanAct" onclick = "wanConnectAct (event, 'ppp0', 'disconnect')">


如果以这种方式进行操作,并且在JavaScript中正确定义了点击处理程序,它将可以正常工作:


If you do it this way, it will work, if you define your click handler in JavaScript properly:

function wanConnectAct(
   eventObject, // event information will be passed (mouse coordinates, etc.)
   id, // "ppp0" will be passed
   value) // { "" will be passed
      // ...
}


在此函数内部,您可以使用id标识按钮对象(请参见下文),但这并不是必需的,因为您已经传递了value.同时,您似乎尝试使用该值来标识操作的类型.如果是这样,这是错误的方法,不利于维护.这是魔术字" 反模式.显然,您必须在多个地方写这个词,这将破坏代码的可维护性.如果只在一个地方拼错了怎么办?很难检测到.请参阅: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [ ^ ].

另一种(更简单的?)方法是在JavaScript中添加事件处理程序.这样,您将不需要任何东西来标识按钮对象或任何其他目标:


Inside this function, you can identify the button object using the id (see below), but it is hardly needed, because you already passed the value. At the same time, it looks like you try to use the value to identify the type of action. If so, this is wrong approach, bad for maintenance. This is the "magic word" anti-pattern. Apparently, you have to write this word in more than one place, which will break the maintainability of your code. What if you misspell it in just one place? It will be hard to detect. Please see: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

A different (simpler?) approach is to add the event handler in JavaScript. In this way, you won''t need anything to identify your button object or any other target:

myButton = document.getElementById("ppp0");
// already identified, now use the object reference:
myButton.onclick = function() {
    // do whatever you want;
    // can call here any other (named) function
    // and pass any arguments
}


仅记住,您必须在body加载事件(onload事件属性)的处理程序中调用此代码,以及所有其他获得DOM对象引用并添加事件处理程序(以及更多)的代码. />
在这种方法中,您无需使用按钮的value来标识操作;和纯JavaScript.然后,您可以在功能上简单到枚举类型的对象中定义所有动作,并在所有位置使用它,而不是魔术词,而是引用该对象成员.该对象可能类似于


Only remember that you have to call this code in the handler of the body load event (onload event attribute), as well as all other code getting DOM object references and adding event handlers (and a lot more).

In this approach, you don''t need to use the value of button to identify the action; and pure JavaScript. Then you can define all action in an object functionally simple to enumeration type and use it everywhere, referring to this object members, instead of magic words. This object could be something like

var actions = {connect: 1, disconnect: 2, somethingElse: 3}



即使出现一些错误,代码也将更易于支持.

—SA



Even in case of some bugs, the code will be much easier to support.

—SA


这篇关于帮助Onclick Vb.Net-创建要单击的代码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 06:05