试图在没有事件的情况下调用jquery函数

时间:2012-10-25 13:34:58

标签: javascript jquery

所以将图像转换为像这样的链接

var imgCell = '<a href="javascript:storeInfo(&quot;text&quot;,&quot;text&quot;,&quot;ActiveProjects&quot;);"><img src="https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_open.png"></a>';

这将调用storeInfo函数,该函数将采用“text”“text”和“activeprojects并将它们设置为全局变量,以便它们可以被多个javascript函数使用...

function storeInfo (filePath, webAddress, projectStatus){
theFilePath = filePath;
theWebAddress = webAddress;
controlButton(projectStatus);}

然后在storeInfo函数中我调用这个函数......

function controlButton (projectStatus){
$('#'+projectStatus+' tbody td img').live('click', function () {
    var theTable = ActiveProjectsTable;

    var nTr = this.parentNode.parentNode.parentNode;
    if ( this.src.match('details_close') )
        {
            // This row is already open - close it 
            this.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_open.png";
            theTable.fnClose( nTr );
        }
    else
        {
            // Open this row 
            this.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_close.png";
            theTable.fnOpen( nTr, fnFormatDetails(theTable, nTr), 'details' );
        }
});
}

所以第一次点击img就会调用store info函数,然后调用controlButton函数......然后在控制按钮函数中有jquery代码函数需要再次点击...我想知道如果有办法调用jquery函数没有事件,那么我不需要2次点击。

一旦调用controlButton,我怎样才能调用jquery函数?

1 个答案:

答案 0 :(得分:1)

function controlButton (projectStatus){
    // save the function into a variable
    var funct = function () {
        var theTable = ActiveProjectsTable;

        var nTr = this.parentNode.parentNode.parentNode;
        if ( this.src.match('details_close') )
        {
            // This row is already open - close it 
            this.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_open.png";
            theTable.fnClose( nTr );
        }
        else
        {
            // Open this row 
            this.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_close.png";
            theTable.fnOpen( nTr, fnFormatDetails(theTable, nTr), 'details' );
        }
    }
    // Retrieve the DOM node
    var node = $('#'+projectStatus+' tbody td img');

    // Apply the event listener to the node
    node.live('click', funct);

    // Call the function, with the retrieved node as the call instance('this')
    funct.call(node);
}