简体   繁体   English

Jquery动态更改函数名称

[英]Jquery dynamically change function name

We written some function in jquery.When user click on each div we need to call each function dynamically. 我们在jquery中编写了一些函数。当用户点击每个div时,我们需要动态调用每个函数。

   <div class="test" id="test1"></div>
   <div class="test" id="test2"></div>


function test1(){
   alert('test1');
   }

   function test2(){
   alert('test2');
   }


   $(".test").on("click",function(){

   var function_name=$(this).attr("id");
    window[function_name]();

   });

But this code is not working end the error is window[function_name] is not a function . 但是这段代码不能正常运行,错误是窗口[function_name]不是函数。 How to solve this 怎么解决这个问题

also Please suggest another 2,3 methods 还请建议另外2,3种方法

You can simply use window, It will work fine 你可以简单地使用窗口,它会工作正常

 function test1(){ alert('You clicked on Test1'); } function test2(){ alert('You clicked on Test2'); } $(".test").on("click",function(){ var function_name=$(this).attr("id"); var call_myFun = window[function_name]; call_myFun() }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test" id="test1">Test 1</div> <div class="test" id="test2">Test 2</div> 

And You can do this way also.. 你也可以这样做..

 $(".test").on("click",function(){ var function_name=$(this).attr("id"); eval(function_name + "()"); }); function test1(){ alert('clicked on test1'); } function test2(){ alert('clicked on test2'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test" id="test1">test1</div> <div class="test" id="test2">test2</div> 

Umm.. You can create summary function to all your divs : 嗯..你可以为你的所有div创建汇总功能:

JQuery: JQuery的:

function xDiv(e){
    var i = $(e).attr("id");
    switch (i){
        case "test1":
             //Call matching function
            break; 
        case "test2":
            //Call matching function
            break; 
        case "test3":
            break;
        /*...*/
        default:
            break;
    }
     alert("This call from: " + i );
}

**HTML: ** ** HTML:**

<div id="test1" onclick="xDiv(this);">test1</div>
<div id="test2" onclick="xDiv(this);">test2</div>
<div id="test3" onclick="xDiv(this);">test3</div>
<div id="test4" onclick="xDiv(this);">test4</div>

instead of 代替

var function_name=$(this).attr("id");
function_name();

use 使用

var function_name=$(this).attr("id");
eval(function_name+"()");

see example https://jsfiddle.net/f6z04dfq/ 参见示例https://jsfiddle.net/f6z04dfq/

On my point of view the approach to the problem is wrong. 在我看来,解决问题的方法是错误的。

If you have an id on your div you could bind the proper function to the event you desire. 如果你的div上有id ,你可以将正确的函数绑定到你想要的事件上。

That is the simple and effective solution to your problem. 这是解决您问题的简单而有效的方法。

The advantage on readability and maintainability is huge. 可读性和可维护性的优势是巨大的。

For example: what if some one in the future will change the name on a function in a way that the id don't match any more? 例如:如果将来某个人以某种方式更改某个功能上的名称,而该ID不再匹配,该怎么办?

It is not easy to find the piece of code where the function is dynamically called. 找到动态调用函数的代码段并不容易。

So here is my proposal: 所以这是我的建议:

 function test1() { console.log("Click on DIV 1"); } function test2() { console.log("Click on DIV 2"); } $(function(){ $('#test1').click(test1); $('#test2').click(test2); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test" id="test1">Test 1</div> <div class="test" id="test2">Test 2</div> 

you can call dynamic function using call method. 你可以使用call方法调用动态函数。

window[fun].call();

so in your case 所以在你的情况下

$(".test").on("click",function(){
   var function_name=$(this).attr("id");
   window[function_name].call();
});

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

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