Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将HTML元素传递给JavaScript函数_Javascript_Jquery_Html - Fatal编程技术网

将HTML元素传递给JavaScript函数

将HTML元素传递给JavaScript函数,javascript,jquery,html,Javascript,Jquery,Html,我将3个html元素作为参数传递给JS函数。JS函数位于单独的文件中。我无法将“click”事件与_confBtn对象(即参数)绑定。我的完整JS文件: window.HAS = window.HAS || {}; HAS.MyApp = HAS.MyApp || {}; (function (_this, $, undefined) { var _sessionTimeOut = false; var _startCountDown = false; var _counterTime; v

我将3个html元素作为参数传递给JS函数。JS函数位于单独的文件中。我无法将“click”事件与_confBtn对象(即参数)绑定。我的完整JS文件:

 window.HAS = window.HAS || {};
HAS.MyApp = HAS.MyApp || {};

(function (_this, $, undefined) {
var _sessionTimeOut = false;
var _startCountDown = false;
var _counterTime;
var _countDownTime;
var _dialogWrap;
var _confBtn;
var _counter;

_this.init = function (showDialogTime, logofCountDownTime, dialogWrap, counter, confirmationButton) {

    _counterTime = 5;
    _countDownTime = 0;
    _dialogWrap = $('#' + dialogWrap);
    _confBtn = $('#' + confirmationButton);
    _counter = $('#' + counter);
    alert(_confBtn.text());
    createSessionTimeOut();
    $(document).bind("mousemove keypress mousedown mouseup", resetTimeOut);
}

_confBtn.on('click', function () {
    window.clearInterval(_startCountDown);
    _dialogWrap.css('visibility', 'hidden');
    createSessionTimeOut();
    $(document).bind("mousemove keypress mousedown mouseup", resetTimeOut);
});

function createSessionTimeOut() {
    _sessionTimeOut = window.setTimeout(function () {

        _dialogWrap.removeAttr("style");
        _counter.text(_counterTime);
        $(document).unbind("mousemove keypress mousedown mouseup");

        startCountDown();
    }, 2000);
}

function startCountDown() {
    _startCountDown = window.setInterval(function () {
        if (_counterTime >= 0) {
            _counter.text(_counterTime--);
        }
        _countDownTime++;
        if (_countDownTime >= 4) {
            logOutUser();
            return;
        }

    }, 1000);
}

function resetTimeOut() {
    window.clearTimeout(_sessionTimeOut);
    _sessionTimeOut = false;
    createSessionTimeOut();
}

function logOutUser() {
    $.ajax({
        url: '/MyApp/Account/LogOut',
        type: 'GET',
        success: function () {
            document.location.href = '/MyApp/Account/Login';
        }
    })
}

}(window.HAS.MyApp.SessionTimeOut = window.HAS.MyApp.SessionTimeOut || {}, jQuery));
我在单独的页面中调用,如下所示:

SessionTimeOut.init('5', '5', 'dialog-wrap', 'confirm-button', 'counter');
当我尝试调用click event时,我遇到了_confBtn问题。未定义的浏览器显示


请提供帮助。

最好做一些更具活力的事情,比如:

function SomeFunction (element1,element2) {
    var e1 = $("#"+element1),
        e2 = $("#"+element2);
    // Do something with variables e1 and e2
}
//html:
<div id="one"><div>
<div id="two"><div>

//javasctript:
SomeFunction('one','two');
你会这样称呼我:

function SomeFunction (element1,element2) {
    var e1 = $("#"+element1),
        e2 = $("#"+element2);
    // Do something with variables e1 and e2
}
//html:
<div id="one"><div>
<div id="two"><div>

//javasctript:
SomeFunction('one','two');
//html:
//javasctript:
SomeFunction('one','two');

您当然可以这样做,只需使用包含多个选择器的普通jQuery方式即可。您的代码有点不正确,因为您实际上只定义了函数而没有调用它,并且在定义函数时不应该将参数/变量传递给函数

除非您打算区分两组元素,否则我不会像您在问题中使用的那样单独声明元素,因为有时您永远不会知道所选项目的长度

function someFunction($ele) {
    // jQuery objects will be accessible as $ele
}

// Call the function
someFunction($('#selector1, #selector2'));
但是,如果是前者,您始终可以这样做:

function someFunction($ele1, $ele2) {
    // jQuery objects will be accessible as $ele1 and $ele2 respectively
    // Example: $ele1.hide();
    //          $ele2.show();
}

// Call the function
someFunction($('#selector1'), $('#selector2'));
例如,您可以参考以下概念验证JSFIDLE:


若要将某些元素传递给函数,可以使用jQuery构造函数来标准化参数

function SomeFunction (element1,element2) {
    element1 = $(element1);
    element2 = $(element2);
    // and you have 2 jQuery objects...
}

// and now you can pass selector as well as jQuery object.
SomeFunction($('div.a'),'#b');

不,您正在以某种方式混合函数声明和函数调用。定义函数时不能提供函数参数。但是,这将很好地工作:

function someFunction($element1, $element2) {
    //Do something with the elements
}

someFunction($("#element1"), $("#element2"));

请注意,
$element1
$element2
只是变量名,前面的$与jQuery没有任何关系。识别引用jQuery选择的变量只是一种常见的约定。

是否可能?对可以将HTML元素和jQuery对象同时传递给JavaScript方法,也可以不传递。但是,您不能在函数定义中包含jQuery方法调用——请记住,
$()
是一个函数调用。值得一提的是,您并没有将HTML传递给函数,而是将jQuery选择(表示元素)反过来表示一些HTML标记。真是不可思议!我使用$('#container').delegate('a','click',function(){alert(“thattickles!”)});而不是点击,它的工作。根据明天的报道,我将更详细地回顾委托的工作方式。通常我会将单击绑定到我的按钮上,如:$(document).delegate(“#确认按钮”,“单击”,函数(){});为什么假设将两个元素放在同一个jQuery集合中是合适的?如果要在函数中对两个选择进行不同的处理,那么将它们作为单独的参数传递可能有很好的理由。@PeterHerdenborg为什么假设不是这样呢?OP并没有具体说明是否需要将两者分开或加以区分。但通过这条推理路线,你可以假设问题中没有明确定义的任何东西,从而更难区分答案的本质和你选择用来增加答案趣味的各种东西。但我同意,值得一提的是,有可能合并成一个JQ选择,因为OP可能没有意识到这点。@PeterHerdenborg你应该真的,真的控制住你的东西,伙计。我们都是来帮助你们的,我真的不明白你们为什么对我们如何(区别地)解释OP的问题如此吹毛求疵。每个人都有自己的。是的,当然,我们都是来帮忙的。一种方法是对答案进行评论,既可以改进答案,也可以添加对阅读答案的人可能有帮助的信息。我不认为我上面的两条评论有任何极端或攻击性。我还注意到,在我第一次发表评论之后,您很高兴地更新了您的答案。无需就此展开争论。请添加2-3行解释,而不仅仅是纯代码,因为您的答案会在低质量队列中弹出,并且没有文本解释,可能有一天会关闭。谢谢。是否可以调用。单击我作为参数传递的html元素下的事件?是的,您可以在函数中使用变量
e1
,就像您在代码中的任何其他地方使用
$('#one')
,因此您可以绑定到它,修改它等等。但是我使用了类似于e1的方法。单击(函数(){//Do something}),但它不起作用。e1未定义。您可以在底部查看我的消息以了解详细信息。请查看底部的最后一个答案。我很无聊:(