2017-04-22 50 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

相關問題