用JavaScript命名JavaScript函数?

时间:2021-11-07 09:04:56

I was trying the next code without success

我正在尝试下一个代码而没有成功

HTML

<a id="addBookButton" href="javascript:showForm('addBookButton','add-book','activateAddBookForm');" class="addA"><span>Add Book</span></a>

Javascript


function showForm(button,form,callback) {
    $("#"+button).hide();
        $("#"+form).show();
        callback();
}

6 个解决方案

#1


You can just pass a reference to the function into your showForm function.

您只需将对函数的引用传递给showForm函数即可。

<a id="addBookButton" href="javascript:showForm('addBookButton','add-book',activateAddBookForm);" class="addA"><span>Add Book</span></a>

#2


Try this:

function showForm(button,form,callback) {
    $("#"+button).hide();
    $("#"+form).show();
    if (typeof this[callback] == "function") this[callback]();
}

Of you pass the function by value and not just the name of it:

你通过值传递函数而不仅仅是它的名称:

<a id="addBookButton" href="javascript:showForm('addBookButton','add-book',activateAddBookForm);" class="addA"><span>Add Book</span></a>

#3


Change your a tag to:

将您的标记更改为:

<a id="addBookButton" href="#" onclick="showForm('addBookButton','add-book','activateAddBookForm'); return false;" class="addA"><span>Add Book</span></a>

Note the onclick handler, and the return false; within the onclick.

注意onclick处理程序,返回false;在onclick内。

#4


function showForm(button,form,callbackName) {
    $("#"+button).hide();
    $("#"+form).show();
    var callback = window.callbackName;
    if(typeof callback === 'function') {
        callback();
    }
}

#5


<a id="addBookButton" onclick="javascript:showForm('addBookButton','add-book','activateAddBookForm');" class="addA"><span>Add Book</span></a>

Javascript

function showForm(button,form,callback) {
  // Your Code
}

function callFunction(){
  document.getElementById('addBookButton').onclick();
}

#6


One option is to pass the function be reference instead of as a string, so:

一种选择是将函数传递给引用而不是字符串,因此:

... javascript:showForm('addBookButton','add-book', activateAddBookForm); ....

or to continue using the string:

或继续使用字符串:

function showForm(button,form,callback) {
    $("#"+button).hide();
        $("#"+form).show();
        new Function(callback + '();')();
}

#1


You can just pass a reference to the function into your showForm function.

您只需将对函数的引用传递给showForm函数即可。

<a id="addBookButton" href="javascript:showForm('addBookButton','add-book',activateAddBookForm);" class="addA"><span>Add Book</span></a>

#2


Try this:

function showForm(button,form,callback) {
    $("#"+button).hide();
    $("#"+form).show();
    if (typeof this[callback] == "function") this[callback]();
}

Of you pass the function by value and not just the name of it:

你通过值传递函数而不仅仅是它的名称:

<a id="addBookButton" href="javascript:showForm('addBookButton','add-book',activateAddBookForm);" class="addA"><span>Add Book</span></a>

#3


Change your a tag to:

将您的标记更改为:

<a id="addBookButton" href="#" onclick="showForm('addBookButton','add-book','activateAddBookForm'); return false;" class="addA"><span>Add Book</span></a>

Note the onclick handler, and the return false; within the onclick.

注意onclick处理程序,返回false;在onclick内。

#4


function showForm(button,form,callbackName) {
    $("#"+button).hide();
    $("#"+form).show();
    var callback = window.callbackName;
    if(typeof callback === 'function') {
        callback();
    }
}

#5


<a id="addBookButton" onclick="javascript:showForm('addBookButton','add-book','activateAddBookForm');" class="addA"><span>Add Book</span></a>

Javascript

function showForm(button,form,callback) {
  // Your Code
}

function callFunction(){
  document.getElementById('addBookButton').onclick();
}

#6


One option is to pass the function be reference instead of as a string, so:

一种选择是将函数传递给引用而不是字符串,因此:

... javascript:showForm('addBookButton','add-book', activateAddBookForm); ....

or to continue using the string:

或继续使用字符串:

function showForm(button,form,callback) {
    $("#"+button).hide();
        $("#"+form).show();
        new Function(callback + '();')();
}