简体   繁体   English

如何访问通过JavaScript函数传入的变量

[英]How to access a variable passed in through JavaScript function

I have the following button setup: 我有以下按钮设置:

<div class="BlogManagementDiv">
     <button type="button" onclick="fnToggleBox(AddcategoryDiv)">Add Category</button>                                                    
        <div id="AddcategoryDiv" class="BlogManagementcontainer">
...
        </div>
</div> 

Note the onclick does this: 注意onclick会这样做:

fnToggleBox(AddcategoryDiv)

Where it is passing in a reference to the div below: 它传递对以下div的引用:

AddcategoryDiv

I then access the element in the funciton like this: 然后,我像这样访问函数中的元素:

    function fnToggleBox(element) 
    {
        if (!$('#'+element.id).is(":visible")) {
            $('#' + element.id).show();
        }
        else {
            $('#' + element.id).hide();
        }
    }

which I can see is completeley ridiculous, as it already has the element, and there is no need to acces it in this way. 我看到的是completeley荒谬的,因为它已经包含了元素,并且不需要以这种方式进行访问。

My question is, how to properly access this variable using JQuery. 我的问题是,如何使用JQuery正确访问此变量。

I know how to do using JavaScript, but am trying to do so using JQuery. 我知道如何使用JavaScript,但是正在尝试使用JQuery。

I have tried: 我努力了:

function fnToggleBox(element) 
    {
        if (!element).is(":visible")) {
            element.show();
        }
        else {
            element.hide();
        }
    }

and

function fnToggleBox(element) 
    {
        if (!$element).is(":visible")) {
            $element.show();
        }
        else {
            $element.hide();
        }
    }

but they do not work. 但它们不起作用。

I have googled, but cannot find the answer. 我已经用谷歌搜索,但是找不到答案。

Description 描述
You simply need to wrap the element to access the DOM. 您只需要包装元素即可访问DOM。

<button type="button" onclick="fnToggleBox(AddcategoryDiv)">Add Category</button>                                                    

Solution
I believe you are looking to simply .toggle the visibility. 我相信您只是想简单地.toggle可见性。 You can do this via the .toggle() function in jQuery. 您可以通过jQuery中的.toggle()函数执行此操作。

Like so: 像这样:

function fnToggleBox(element) 
{
  $(element).toggle();
}

Which isn't really worth writing a function wrapper for, but enjoy! 编写功能包装器确实不值得,但是请尽情享受!


Documentation 文献资料

http://api.jquery.com/toggle/ http://api.jquery.com/toggle/

Description: Display or hide the matched elements. 描述:显示或隐藏匹配的元素。
.toggle( [duration ] [, complete ] ) .toggle([持续时间] [,完成])

jquery wraps dom elements in the $ Jquery function, once these dom elements are wrapped you get all those nice jquery methods but the underlying dom element remains, jquery在$ Jquery函数中包装了dom元素,一旦这些dom元素被包装,您将获得所有这些不错的jquery方法,但底层的dom元素仍然存在,

you are close , just pass the node to the $ function as argument: 您很接近,只需将节点作为参数传递给$函数:

 function fnToggleBox(element) { console.log(element) console.log($(element)) console.log($('#' + element.id)[0] === element) if (!$(element).is(":visible")) { $(element).show(); } else { $(element).hide(); } } 
 #AddcategoryDiv{ height:10px; width:10px; background:red } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="BlogManagementDiv"> <button type="button" onclick="fnToggleBox(AddcategoryDiv)">Add </button> <div id="AddcategoryDiv" class="BlogManagementcontainer"> </div> </div> 

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

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