如何将参数传递给JQuery事件处理程序

时间:2015-12-17 18:17:51

标签: javascript jquery

如何在调用中传递参数,如下所示?:

PS C:\Users\me\chef-repo> knife ssl fetch
WARNING: Certificates from api.chef.io will be fetched and placed in your trusted_cert
directory (c:\users\me\chef-repo\.chef\trusted_certs).

Knife has no means to verify these are the correct certificates. You should
verify the authenticity of these certificates after downloading.

Adding certificate for *.opscode.com in c:\users\me\chef-repo\.chef\trusted_certs/wildcard_opscode_com.crt
Adding certificate for DigiCert SHA2 Secure Server CA in c:\users\me\chef-repo\.chef\trusted_certs/DigiCert_SHA2_S
ecure_Server_CA.crt



PS C:\Users\me\chef-repo> knife ssl check
Connecting to host api.chef.io:443
ERROR: The SSL certificate of api.chef.io could not be verified
Certificate issuer data: /C=US/O=DigiCert Inc/CN=DigiCert SHA2 Secure Server CA

Configuration Info:

OpenSSL Configuration:
* Version: OpenSSL 1.0.1l 15 Jan 2015
* Certificate file: C:/projects/openssl/knap-build/var/knapsack/software/x86-windows/openssl/1.0.1p/ssl/cert.pem
* Certificate directory: C:/projects/openssl/knap-build/var/knapsack/software/x86-windows/openssl/1.0.1p/ssl/certs
Chef SSL Configuration:
* ssl_ca_path: nil
* ssl_ca_file: "C:/opscode/chefdk/embedded/ssl/certs/cacert.pem"
* trusted_certs_dir: "c:\\users\\me\\chef-repo\\.chef\\trusted_certs"

TO FIX THIS ERROR:

If the server you are connecting to uses a self-signed certificate, you must
configure chef to trust that server's certificate.

By default, the certificate is stored in the following location on the host
where your chef-server runs:

  /var/opt/opscode/nginx/ca/SERVER_HOSTNAME.crt

Copy that file to your trusted_certs_dir (currently: c:\users\me\chef-repo\.chef\trusted_certs)
using SSH/SCP or some other secure method, then re-run this command to confirm
that the server's certificate is now trusted.

PS C:\Users\me\chef-repo>

如果$('nav .navbar-nav .level-0').on('mouseenter', toggle_level0_menu); function toggle_level0_menu(/*parameters*/) { // code here } 需要接收参数,是否有办法从内联呼叫中传递它?我知道以下是一个解决方案:

toggle_level0_menu

但这正是我想要尽可能避免的。

4 个答案:

答案 0 :(得分:1)

您必须在匿名函数中包装函数调用。 如果将参数传递给命名函数,则调用它并返回值,而不是回调。所以你说:

$('nav .navbar-nav .level-0').on('mouseenter', function(){
   toggle_level0_menu(/*parameters*/);
});

编辑:

如果您希望传递给回调的参数来自事件,您可以编写回调函数toggle_level0_menu(/*parameters*/)以便它接收事件,并且您可以通过该方式访问事件数据。

e.g。

function toggle_level0_menu(event) {
   console.log(event.target);
}

 $('nav .navbar-nav .level-0').on('mouseenter', toggle_level0_menu);

查看jquery on的文档。您还可以委派给将来的DOM节点以及传入event.data对象。

答案 1 :(得分:1)

是的可能

将参数传递给jQuery事件处理程序。传递一个数据对象作为事件的第二个参数,该对象的内容将被转移到事件的数据变量上。

检查下面的工作示例。

希望有所帮助。

$('.level-0').on('mouseenter', {msg: 'test'}, toggle_level0_menu);

function toggle_level0_menu(e){
     console.log(e.data.msg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='level-0'>level-0</div>

答案 2 :(得分:0)

没有一种情况可以将参数传递给这样的函数,因为它被窗口调用(它为函数提供了自己的参数)

如果函数的行为有所不同,请在函数内对其进行编码。

虽然看看.bind()。call()和.apply()

答案 3 :(得分:0)

您可以使用jquery on将数据传递给函数。

同样是here

的示例
$("#myid").on("click",myfunction.bind(this, arg1, arg2));