gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:38:22 24 4
gpt4 key购买 nike

我正在尝试使数据表在单击时展开以显示更多信息。问题是我通过大约 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>';

这样就生成了一个在单击时调用此函数的图像链接...

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

switch(projectStatus){
case "New Work - Waiting Activation":
controlButton("NewWork", NewWorkTable);
break;
case "Active Projects":
controlButton("ActiveProjects", ActiveProjectsTable);
break;
case "Waiting for Assets or Approvals":
controlButton("WaitingAssets", WaitingAssetsTable);
break;
//AND A WHOLE LOT MORE CASES
}
}

基本上,该函数将文件路径和网址保存到全局变量中以供稍后使用,然后调用 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 函数。我需要一种方法来解决这个问题,只需单击一下即可,同时仍然为每个单独的条目加载数据...我认为最好的方法是找到一种无需 即可调用 jQuery 函数的方法。 live('click', function() 因为这样就不需要第二次点击。

有什么建议吗?

最佳答案

按如下方式更新您的函数

storeInfo() 声明:

function storeInfo (filePath, webAddress, projectStatus, elem)
{ //code }

调用链接:

<a href="javascript:storeInfo("'+theFilePath+'","'+theWebAddress+'","'+projectStatus+'", this);"><img src="..."></a>

while 内对 controlButton() 的调用:

controlButton("ActiveProjects", ActiveProjectsTable, elem);

并更新了 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' );
}
}

无法测试,但应该可以。

关于javascript - 如何在没有事件的情况下调用 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13072319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com