简体   繁体   English

如何使用jQuery更改全局变量并将其传递给函数的参数?

[英]How to change global variables and pass them to a function's parameters with jQuery?

I am trying to pass two variables to a pre-written function that has 2 parameters. 我试图将两个变量传递给具有2个参数的预写函数。 The idea is when a users clicks on a filter variable 1 gets set, then when they click on an item variable 2 gets set. 这个想法是当用户单击过滤器变量1时设置的,然后当用户单击项目变量2时设置的。 Once they click on an item it sets the two variables as the parameters in a pre-written function (I cannot gain access to change this function). 一旦他们单击一个项目,它将在预编写函数中将这两个变量设置为参数(我无法获得更改此函数的权限)。 Here is what I have. 这就是我所拥有的。

HTML Filters: HTML过滤器:

<a href="#" data-filter="*" class="current filter">All</a>
<a href="#" data-filter=".filter1" class="filter">filter1</a>
<a href="#" data-filter=".filter2" class="filter">filter2</a>
<a href="#" data-filter=".filter3" class="filter">filter3</a>

HTML Links: HTML链接:

 <a data-track="Track Var Link1" href="link1.aspx">Link 1</a>
 <a data-track="Track Var Link2" href="link2.aspx">Link 2</a>
 <a data-track="Track Var Link3" href="link3.aspx">Link 3</a>

jQ: jQ:

var filterText = 'all';
var trackText = '';

$('a.filter').click(function(event) {
    filterText = $(this).data('filter');
});

$('.item a').click(function(event) {
    trackText = $(this).data('track');
});

$('.item a').click({param1: filterText, param2: trackText}, preWrittenFunction);

Pre-Written-Function: 预写功能:

function preWrittenFunction(type, lname) {
    s = s_gi(s_account);
    s.linkTrackVars = 'prop5,prop17,prop18,pageName'
    s.linkTrackEvents = 'None';
    s.prop5 = document.URL.toLowerCase(); // url
    s.prop17 = s.pageName; // name and campaign pathing
    s.prop18 = type; // widget type
    s.tl(this, 'o', type + ": " + lname); // link name includes type
}

Wrap the call to your preWrittenFunction in an anonymous function to be used as click event handler: 将对您的preWrittenFunction的调用包装在一个匿名函数中,以用作click事件处理程序:

$('.item a').click(function() {
  preWrittenFunction(filterText, trackText);
});

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

相关问题 如何将变量作为全局 arguments 传递给 IIFE function? - How to pass variables to IIFE function as global arguments? jQuery 的 .click - 将参数传递给用户函数 - jQuery's .click - pass parameters to user function 是否可以将变量传递给 jquery 的 css 函数? - Is it possible to pass variables to jquery's css function? 在调用使用回调的函数(在循环中)时,如何传递变量并使它们不变? - When calling a function (in a loop) which uses a callback, how pass variables in and have them not change? 如何编写函数并传递jQuery函数的参数 - How to write function and pass parameters for a jquery function 如何将所有复选框ID传递给jQuery更改功能 - How to pass all checkbox id's to jQuery change function 将值从.change函数中的变量传递和更新为全局变量Jquery - Pass and update values from variable in .change function to global variable Jquery 如何将动态参数传递给jquery函数 - how to pass dynamic parameters to jquery function 如何将参数传递给jquery警报功能 - How to pass parameters to the jquery alert function 如何保存JavaScript变量并使用jQuery将它们传递到下一页? - How to save JavaScript variables and pass them to the next page with jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM