Javascript 如何在没有事件的情况下调用jQuery函数

Javascript 如何在没有事件的情况下调用jQuery函数,javascript,jquery,datatables,jquery-events,Javascript,Jquery,Datatables,Jquery Events,我正试图让dataTable在单击时展开以显示更多信息。问题是,我正在通过大约1000条记录填充该表,因此我可以保存更多详细数据(扩展的内容)的唯一方法是在循环过程中通过链接发送数据,如 var imgCell = '<a href="javascript:storeInfo(&quot;'+theFilePath+'&quot;,&quot;'+theWebAddress+'&quot;,&quot;'+projectStatus+'&a

我正试图让dataTable在单击时展开以显示更多信息。问题是,我正在通过大约1000条记录填充该表,因此我可以保存更多详细数据(扩展的内容)的唯一方法是在循环过程中通过链接发送数据,如

var imgCell = '<a href="javascript:storeInfo(&quot;'+theFilePath+'&quot;,&quot;'+theWebAddress+'&quot;,&quot;'+projectStatus+'&quot;);"><img src="https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_open.png"></a>';
基本上,此函数将文件路径和webaddress保存到全局变量中,以供以后使用,然后调用
controlButton
函数

function controlButton (projectStatus, theTable){
$('#'+projectStatus+' tbody td img').live('click', function () {
     
    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' );
        }
     });
}
这样做的目的是在html表中获取img,然后在其中查找行,然后调用一个函数来打开或关闭可扩展数据。。。这是通过
fnOpen
fnClose
完成的,并使用
fnFormatDetails
填充可展开行

这一切都很好,花花公子和一切都得到了正确的传递和加载伟大的,但唯一的问题是,它需要两次点击打开和关闭可扩展行。。。一次用于图像上的链接,然后另一次用于调用jQuery函数。我需要一种方法来绕过这一点,只需点击一下,同时仍然为每个条目加载数据。。。我想最好的方法是找到一种不用
.live('click',function()
调用jQuery函数的方法,因为这样就不需要再次单击


有什么建议吗?

我不知道我是否正确理解你的问题,但是你能在
storeInfo
函数的末尾调用
controlButton
函数吗?

我不知道我是否正确理解你的问题,但是你能在
storeInfo
函数的末尾调用
controlButt吗在
函数上?

按如下方式更新函数

storeInfo()的声明:

访问链接:

<a href="javascript:storeInfo("'+theFilePath+'","'+theWebAddress+'","'+projectStatus+'", this);"><img src="..."></a>
并更新了controlButton()函数:

function controlButton (projectStatus, theTable, elem){
    img = elem.getElementsByTagName("img")[0]; //alternatively: $(elem).find('img');
    var nTr = elem.parentNode.parentNode;
    if ( img.src.match('details_close') )
    {
        // This row is already open - close it 
        img.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_open.png";
        theTable.fnClose( nTr );
    }
    else
    {
        // Open this row 
        img.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_close.png";
        theTable.fnOpen( nTr, fnFormatDetails(theTable, nTr), 'details' );
    }
}

无法测试,但应能正常工作。

如下更新您的函数

storeInfo()的声明:

访问链接:

<a href="javascript:storeInfo("'+theFilePath+'","'+theWebAddress+'","'+projectStatus+'", this);"><img src="..."></a>
并更新了controlButton()函数:

function controlButton (projectStatus, theTable, elem){
    img = elem.getElementsByTagName("img")[0]; //alternatively: $(elem).find('img');
    var nTr = elem.parentNode.parentNode;
    if ( img.src.match('details_close') )
    {
        // This row is already open - close it 
        img.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_open.png";
        theTable.fnClose( nTr );
    }
    else
    {
        // Open this row 
        img.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_close.png";
        theTable.fnOpen( nTr, fnFormatDetails(theTable, nTr), 'details' );
    }
}

无法测试,但应该可以工作。

我不确定是否正确理解了您的问题,但如果仅从storeInfo调用controlButton(由单击触发),我不明白您为什么需要controlButton中的实时绑定。
如果只是为了获取nTr,您可以将其作为参数从storeInfo传递给controlButton。

我不确定是否正确理解了您的问题,但如果controlButton仅从storeInfo调用(由单击触发),我不明白您为什么需要controlButton中的实时绑定。
如果只是为了获取nTr,您可以将其作为参数从storeInfo传递给controlButton。

我可以,但这不重要,因为“$('.'.'项目状态+'tbody td img')。live('单击',函数()”这需要第二次单击我可以,但这不重要,因为“$('.'项目状态+'tbody td img')。live('单击',函数()”)这需要第二次单击。若我这样做,那个么所有这些变量都会失败…它需要加载“$”(“#”+projectStatus+“tbody td img”)。但当我尝试将其设置为一个变量时,然后执行.parentnode调用它无法完成…我需要一种方法来调用“$”(“#”+projectStatus+“tbody td img”)上的函数通过运行controlButton函数,我认为它不会起作用,因为通过链接传递的信息不是来自$('#projectStatus)表,而是通过Sharepoint services循环发送的,该循环正在读取列表和“this”中的值定义为通过循环传递的当前数据…这是一种非常独特的情况,我无法通过我的帖子解释所有情况。“This”将在javascript:运行时引用锚标记。但该链接中的“This”是javascript This,而不是Sharepoint This。请注意,它没有被引用,因此将在浏览器a中解释s link.img=elem.getElementsByTagName(“img”)的javascript DOM元素[0];这一行给了我一个错误,表示对象并没有那个属性…而另一行说它无法读取该属性。未定义的parentNode…当我提醒img时,它给了我对象对象,所以我认为它不能正常工作@freoni若我这样做,那个么所有这些变量都失败了…它需要加载“$(“#”+projectStatus+“tbody td img”)“但是当我尝试将其设置为一个变量时,然后执行.parentnode调用它是不行的…我需要一种方法来调用“$”(“#”+projectStatus+“tbody td img”)上的函数,只需运行controlButton函数,我认为它不会起作用,因为通过链接传递的不是来自$(“#projectStatus)表正在通过Sharepoint services循环发送,该循环正在从列表和“this”中读取值定义为通过循环传递的当前数据…这是一种非常独特的情况,我无法通过我的帖子解释所有情况。“This”将在javascript:运行时引用锚标记。但该链接中的“This”是javascript This,而不是Sharepoint This。请注意,它没有被引用,因此将在浏览器a中解释s link.img=elem.getElementsByTagName(“img”)的javascript DOM元素[0];这一行给了我一个错误,表示对象没有该属性…另一行说它无法读取该属性。未定义的parentNode…当我提醒img时,它给了我对象对象,所以我认为这工作不正常@freonI可以尝试,但我的问题是获取nTr的值…我似乎无法在没有你的情况下做到这一点t在jquery函数中使用此函数…例如…我尝试了var node=$(“#”+projectStatus+“tbody td img”);var nTr=node.parentNode.parentNode.parentNode;但是我得到一个错误,说不能做。parentNode在未定义上…看一看freon的更新答案,这就是我的想法。我可以尝试,但我的问题是得到nTr的值…如果不在jquery函数中使用这个函数,我似乎无法做到这一点…例如…我尝试了var node=$('#'+projectStatus+'tbody td img');变量nTr=n
function controlButton (projectStatus, theTable, elem){
    img = elem.getElementsByTagName("img")[0]; //alternatively: $(elem).find('img');
    var nTr = elem.parentNode.parentNode;
    if ( img.src.match('details_close') )
    {
        // This row is already open - close it 
        img.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_open.png";
        theTable.fnClose( nTr );
    }
    else
    {
        // Open this row 
        img.src = "https://cubistmediagroup.sharepoint.com/sites/canvas/PublishingImages/details_close.png";
        theTable.fnOpen( nTr, fnFormatDetails(theTable, nTr), 'details' );
    }
}