简体   繁体   English

函数在“ $(function(){”内部工作,但不在外部工作吗?

[英]Function works inside “$(function() {” but not outside?

Here's what the function looks like: 该函数的外观如下:

function changeWidth(el,event) {

        event.preventDefault();

        if($('#RightSide').is(':visible')){
           $('#RightSide').delay(1000).fadeOut(0);            
        }
        else{
           $('#RightSide').fadeIn(0);
        }


        var toggleWidth = $("#LeftPanel").width() == 365 ? "1096px" : "365px";
        /* $('#LeftPanel').width(toggleWidth);*/
        $('#LeftPanel').animate( {'width': toggleWidth}, 1000);  

        if($('#LeftPanel').is('.LeftPanelRadius')){
            $('#LeftPanel').removeClass('LeftPanelRadius'); return;
        }

        $('#LeftPanel').addClass('LeftPanelRadius');

    } 

It's supposed to activate when someone clicks on an image on my website. 当有人单击我网站上的图像时,该按钮应该会激活。 Here is the HTML code for that: 这是该代码的HTML代码:

<img src="images/magCircy.png" width="33px" height="33px" onClick="changeWidth(this,event);"></img>      

The jQuery and HTML code work great, but it only works if the jQuery code is OUTSIDE this general thing that all my other functions are in: jQuery和HTML代码很好用,但是只有当jQuery代码超出了我所有其他功能所在的常规范围时,它才有效:

$(function() {

});      

And the reason why I'm trying to move it inside that general thing, is because I want function changeWidth to call another function that is inside that general thing. 我之所以尝试将其移入该通用对象 ,是因为我希望函数changeWidth调用该通用对象内的另一个函数。 And I am assuming that you can only call functions that are inside that general thing from functions that are also inside it. 我假设您只能从也包含在该通用对象内部的函数中调用该通用对象内部的函数。

...Sorry if this all sounds very convoluted... Please let me know if you can help. ...很抱歉,这听起来很令人费解...请告诉我您能否提供帮助。

Thank you. 谢谢。

And I am assuming that you can only call functions that are inside that general thing from functions that are also inside it. 我假设您只能从也包含在该通用对象内部的函数中调用该通用对象内部的函数。

That's exactly your problem. 那正是你的问题。 You're trying to call your handler from outside it. 您正在尝试从外部调用处理程序。

And it's not a "general thing"; 这不是“一般的事情”; it's an ordinary function that you pass to the $() function as a parameter. 这是一个普通函数,您将其作为参数传递给$()函数。

You should not declare a function inside another function unless it's only used by that function. 除非仅由该函数使用,否则不应在另一个函数内声明该函数。

Right, let's break this down. 是的,让我们分解一下。

You have your element with a handler for an onclick event. 您的元素带有一个onclick事件的处理程序。 As far as the HTML element is concerned, it is looking for function changeWidth on the window object, aka global scope. 就HTML元素而言,它正在window对象(也称为全局范围)上寻找function changeWidth

You also have code defined in $(function(...)); 您还可以在$(function(...));定义代码$(function(...)); which is itself a function, and thus anything inside of it is in its own local scope. 它本身是一个函数,因此其中的任何内容都在其自己的本地范围内。

When your element fires it's click event, it only knows to check window for changeWidth 当您的元素触发它的click事件时,它只知道检查windowchangeWidth

One way around this (though I would not suggest it), is to create a pointer in your jQuery function as such: 解决这个问题的一种方法(尽管我不建议这样做),是在jQuery函数中创建一个指针,如下所示:

$(function () {
    window.changeWidth = function () { /*code*/ };
});

I would suggest keeping with jQuery here though, as it sounds like a good chunk of your code is written in it. 我建议还是在这里使用jQuery,因为听起来好像其中写了很多代码。 That would look something like: 看起来像:

$(function() {
    $('img[src="images/magCircy.png"]').on( 'click', function() {
         //your code here
    });
});

alternatively, you can simply pass your changeWidth function directly into the onClick handler above. 或者,您可以直接将changeWidth函数直接传递到上面的onClick处理程序中。

edit: closing my braces :) 编辑:关闭我的括号:)

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

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