我想要编写JQuery,它将发现页面上是否存在Class="Mandatory",如果有任何元素有这个类,那么只执行特定的操作。
谢谢
发布于 2011-09-12 19:30:32
当你用jQuery搜索它时,只需检查你命中了多少元素
if ($(".Mandatory").length > 0) {
// Do stuff with $(".Mandatory")
$(".Mandatory").each(function() {
// "this" points to current item in looping through all elements with
// class="Mandatory"
$(this).doSomejQueryWithElement();
});
}
编辑如果您想检查您提交按钮点击情况,只需在单击之后进行检查:
$("input[type='submit']").click(function() {
if ($(".Mandatory").length > 0) {
// Do some code here
}
});
发布于 2011-09-12 19:32:22
如果你只想做一次动作,你可以使用:
if ($('.Mandatory').length > 0) {
//do your thing
}
否则,如果您想对每个Mandatory
元素执行此操作:
$('.Mandatory').each(function(){
//do your thing
});
发布于 2011-09-12 19:38:28
要做到这一点,最好的方法是在body标记中搜索类。
$('body').find('.Mandatory').length;
https://stackoverflow.com/questions/7387160
复制相似问题