我試圖讓下面的代碼在我的MVC 5項目的工作:警戒關閉按鈕不Bootstrap與ASP.NET MVC工作
<!-- for the snippet to work -->
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<p>This alert should close, when clicking the 'x'-button:</p>
<div class="alert alert-warning alert-dismissible fade in" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<strong>Holy guacamole!</strong> Best check yo self, you're not looking too good.
</div>
我把它從official bootstrap site。當點擊'x'按鈕時,警報應該關閉。正如你所看到的,它在這段代碼中實際上工作正常。但它不在我的應用程序中。警報確實沒有關閉。很顯然,MVC框架必須爲此負責。
瀏覽器的調試控制檯不顯示任何錯誤消息。是的,我還在項目中加入了Bootstrap(3.5.5)和jQuery(2.1.4)的JS源代碼 - 它們在瀏覽器的調試視圖中正確加載並顯示在'sources'下。
編輯:亞倫建議其他一些腳本可能會覆蓋/隱藏引導代碼。所以這些都是JS庫我的項目目前使用:
- jQuery的2.1.4.min.js
- bootstrap.min.js
- jquery.unobtrusive-ajax.min.js
- 的jQuery -ui-1.11.4.min.js
- jquery.effects.highlight.min.js
- jquery.effects.core.min.js
- 加上其他一些手寫的幫手JS與輔助方法
它們的加載順序與列出的順序相同。遺漏以下庫並沒有幫助:
- 的jQuery-UI-1.11.4.min.js
- jquery.effects.highlight.min.js
- jquery.effects.core.min.js
刪除jquery.unobtrusive-ajax.min.js會導致我的應用程序「崩潰」,因爲它無法再動態加載內容。
下面的匿名函數聲明被調用,當文件被加載(前的警報已被動態插入到HTML):
Alert.prototype.close = function (e) {...}
點擊「X'鍵好像不叫bootstrap.js中的任何代碼,但也許我只是沒有找到放置斷點的正確位置。
你可以創建jsfiddle嗎? – Win
是的,jsfiddle和bootply也可以工作 – wodzu
嗯,它只發生在你的參與項目中。您需要刪除所有JavaScript並將它們重新放回一次。 – Win