
页面嵌套母版页时再用 $("#id") 获取服务器端控件(runat="server") ID,就获取不到了我们可以查看页面源代码,发现ID已经被改了。
我们可以通过:
$("#<%=idClientID %>") 获取服务器控件ID或绑定一些事件。
如:
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>为服务器端文本控件,想要在jquery中取到它客户端的ID需要进行
如下转换:
<script src="/js/jquery-191minjs"></script>
<script type="text/javascript">
$(function () {
var title = $("#<%=txtTitleClientID%>")val();
alert(title);
});</script>
有一个Div,我想获取其ID或者Class值。
那么我可以先定位到这个DIV,然后采用attr方法来获取其值:
假如说:有一段Html代码:
[html] view plain copy
<div class="comment" id="22">
<div class="comment_detail" style="display: block; ">
<div class="comment_my"><textarea class="comment_text" style="height: 16px; "></textarea></div>
<div class="comment_text_bottom" style="display: none; ">
<div class="comment_emotion"><a href="javascript:void(0)" class="comment_emotion_button">表情</a>
<div class="comment_emotion_detail" style="display: none; ">
</div>
</div>
<div class="comment_publish"><input class="submit" type="submit" value="评论"></div>
</div>
</div>
<div class="comment_bar"><a href="javascript:void(0)" class="comment_click"></a></div>
</div>
如果我想从submit处定位,获取到comment处DIv的ID,那么我就可以这样办:
ID值为:
[javascript] view plain copy
$("submit")parent()parent()parent()parent()attr('id');
其值为:22;
CLASS值为:
[javascript] view plain copy
$("submit")parent()parent()parent()parent()attr('class')
其值为:comment;
以上就是关于用jquery如何获取服务器控件的ID全部的内容,包括:用jquery如何获取服务器控件的ID、如何用Jquery获取某一个Div的Class或者ID、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)