2017-04-22 51 views
-3

如何检查元素是否具有特定类并打开模式?检查元素是否具有特定类并打开引导模式

HTML代码:

<ul> 
<li class="class-one class-two has-error">Lorem Ipsum</li> 
</ul> 

如果<li>有类has-error然后引导模式应打开:

<div id="has-error" class="modal fade" role="dialog"> 
    <div class="modal-dialog"> 
    ERROR! 
    </div> 
</div> 

我肯定需要一个脚本来检查类是否存在,并解雇莫塔尔,但我不知道如何开始。

+0

'如果($(元素).hasClass( '的className')){//做你的东西}' – Rajesh

+0

如何发射模态? – RollzRoyce

+2

不难搜索这两个问题....阅读bootstrap文档,看看如何打开模式和搜索*“jQuery有类”*将提供大量的结果...包括API文档 – charlietfl

回答

0

//你需要描述的情况,但如果发生在页面加载它,使用jQuery

$(document).ready(function() { // When page finished loading 
    if ($('.has-error').length) { // if there is an DOM that has class has-error 
    $('#has-error').modal('show'); // Show Modal 
    } 
}); 
+0

我有表单并点击“提交”课程,一些复选框文本标记为红色(如果没有选中,我们会添加类“has-error”)。如果班级,其中红色的颜色,退出并发射一种模式 – RollzRoyce

+0

我想我需要添加“onclick”功能 – RollzRoyce

+0

没有人有想法?:-( – RollzRoyce

-1

您需要以某种方式选择li。在这个例子中,我使用了这个purpouse的'class-class'类。

$('li.class-one').each(function() 
{ 
    if($(this).hasClass('has-error')) 
     $('#has-error').modal('show'); 
}); 
+0

你的选择器甚至不会找到任何匹配,因为它是错误的 – charlietfl

+0

对不起,现在它会工作。我忘了把课程放在里面 – Akaize

相关问题