通过jQuery查找控件ID,并将其传递给函数以切换它

find control id by jquery and pass it to function to toggle it

本文关键字:函数 控件 查找 jQuery ID 通过      更新时间:2023-09-26

通过jquery找到控件ID,并将其传递给函数以切换它。

function toggleDiv() {
   var $usr = $this.find('[id*=shoow]');
   $($usr).toggle('slow');
}
<asp:LinkButton  runat="server" OnClientClick="toggleDiv(); return false;">Detials</asp:LinkButton>
  <table>
    <tr>
      <td id="shoow" style="width:650px;height:100px;background-color:blue; display:none;">
      </td>
    </tr>
</table>

最初,由于您已经声明了服务器端控件,LinkButton ,您必须为其提供一个ID

<asp:LinkButton  ID="linkButtonId" runat="server">Details</asp:LinkButton>

您不必为此使用 find 函数。实际上,我们使用此功能的原因如下:

获取当前匹配集中每个元素的后代 元素,按选择器、jQuery 对象或元素过滤。

你能做什么?

我建议在页面底部正文的结束标记之前添加此脚本

<script type="text/javascript">
    $(function(){
        $("#"+<%=linkButtonId.ClientID%>).click(function(){
            $('#shoow').toggle('slow');
        });
    })
</script>

重要说明

你应该在这个脚本之前加载jQuery。

我想通过查找控制函数或 jquery 从代码隐藏中给出 ID 功能,因为我有数据列表生成 ID 的随机性。

You could select then the element like below:
$("[id*="+"shoow"+"]").toggle('slow');

尝试以下代码片段以了解我的意思:

$(function(){
    $("#buttonId").click(function(){
        $("[id*="+"shoow"+"]").toggle('slow');
    })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="buttonId">Details</button>
<table>
      <tr>
     <td id="3shoow2" style="width:650px;height:100px;background-color:blue; display:none;">
        </td>
    </tr>
</table>

我同意克里斯托斯的回答。

是的,您不必使用find功能

相反,请使用jQuery:

$('#shoow').toggle('slow');