简体   繁体   English

javascript 代码更改 function 的名称

[英]javascript code to change name of function

I have 300 functions with name cloud1=cloud300 that are called onclick on 300 buttons(names of buttons - button1-button300) to change class of 300 textboxes with name tree1-tree300.How can I do this using for loop.我有 300 个名为 cloud1=cloud300 的函数,在 300 个按钮(按钮名称 - button1-button300)上称为 onclick 来更改 300 个名为 tree1-tree300 的文本框的 class。我如何使用 for loop 来执行此操作。

function cloud1(){document.formname.tree1.className="css"}
function cloud2(){document.formname.tree2.className="css"}
function cloud3(){document.formname.tree3.className="css"}

First - create a click handler that works irrespective of which button is clicked - this one figures out the name of the supplied element, and replaces button with tree :首先-创建一个单击处理程序,该处理程序与单击哪个按钮无关-这个处理程序找出所提供元素的名称,并将button替换为tree

function clicker(ev) {
    ev = ev || window.event;
    var el = ev.target || ev.srcElement;
    var name = el.getAttribute('name');
    var tree = name.replace('button', 'tree');
    document.formname[tree].className = 'css';
}

Then, register that same handler on every button然后,在每个按钮上注册相同的处理程序

for (var i = 1; i <= 300; ++i) {
    document.formname['button' + i].onclick = clicker;
}

See http://jsfiddle.net/alnitak/YHv52/ for a working demonstration.有关工作演示,请参阅http://jsfiddle.net/alnitak/YHv52/

Of course, it's easier with jQuery, particularly if you did the right thing and used the id attribute instead of the name attribute:当然,使用 jQuery 会更容易,特别是如果您做了正确的事情并使用id属性而不是name属性:

$('form[name="formname"] :button').live(function() {
    var button = this.id;
    var tree = button.replace('button', 'tree');
    $('#' + tree).addClass('css');
});

Just pass input name as parameter to只需将输入名称作为参数传递给

function button(inputname){
document.formname.inputname.className='css';
}

Assuming all functions are global, this should work:假设所有功能都是全局的,这应该有效:

for (var i=1; i <= 300; i++) {
  this['cloud'+i].call();
}

But as it was said above, please consider having one function and passing arguments to it.但正如上面所说,请考虑拥有一个 function 并将 arguments 传递给它。 300 similar functions are a refactoring opportunity. 300 个类似的功能是一个重构的机会。

Fortunately you don't need to add 300 event handlers for 300 elements.幸运的是,您不需要为 300 个元素添加 300 个事件处理程序。 You can use event delegation and attach only one event handler to an ancestor element (eg the <form> ) which handles all the click events and decides what to do.您可以使用事件委托并将仅一个事件处理程序附加到处理所有点击事件并决定做什么的祖先元素(例如<form> )。 This should be 300x faster .这应该快 300 倍 :) :)

Here is a demo to show you how it works.这是一个演示,向您展示它是如何工作的。

// add one event handler to the <form>
document.formname.onclick = function(e) {
    // normalize event for IE
    e = e || window.event;
    var target = e.target || e.srcElement;
    // if a button was clicked
    if (target.nodeName.toLowerCase() == 'button') {
        // generate tree name
        var name = target.name.replace('button', 'tree');
        // apply css class for this element
        document.formname[name].className = 'css';
    }
};

Note: Assuming that document.formname is in the parent chain of all the buttons注意:假设document.formname在所有按钮的父链中
(it can be simply document ). (它可以是简单的document )。

You can write just one event handler that will take the id of the button (set to the handler name, for extensibility) as param:您可以只编写一个事件处理程序,它将按钮的 id(设置为处理程序名称,以实现可扩展性)作为参数:

<input type="button" id="cloud1" name="test_1" value="Click me"  onclick="handler(this.id)">

<script>

function handler(handler_id)
{
   this[handler_id].call();
}

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

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