2012-10-25 20 views
1

所以車削和圖像轉換成這樣試圖調用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功能,將採取「文本」,「文本」和「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它調用存儲信息函數,該函數返回調用controlButton函數...然後在控制按鈕函數中有jquery代碼函數,需要再次點擊...我想知道是否有一種方法可以在沒有事件的情況下調用jquery函數,因此我不需要點擊兩次。

一旦controlButton被調用,我該如何調用jquery函數?

+0

您的'live'在控件的內部'controlbutton'這隻會在您第一次點擊鏈接時安裝一個事件處理程序。然後它需要第二次點擊來執行安裝了'live'指令的'click'處理程序中的代碼 –

回答

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); 
} 
+0

我仍然需要雙擊來調用「theTable.fnOpen」並關閉函數我正在尋找一種方法來調用只需點擊一下即可打開和關閉表格的單元格 – Kierchon

+0

我們需要查看我的朋友的代碼的更大範例。此外,我最終從我的初始職位編輯了一下,做了一個小錯誤。 – SReject

+0

我添加了商店信息功能,我不認爲它會工作,因爲代碼需要在controlButton函數內單擊並且已經從點擊中被擊中......我試圖調用它而不必再次單擊。 – Kierchon