[英]find control id by jquery and pass it to function to toggle it
通過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');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.