链接到boostrap';的Javascript代码阻止onclick事件处理程序运行?
这个简单的方法是显示一个链接,如果用户点击它,就会弹出一个警报链接到boostrap';的Javascript代码阻止onclick事件处理程序运行?,javascript,jquery,twitter-bootstrap,onclick,jquery-events,Javascript,Jquery,Twitter Bootstrap,Onclick,Jquery Events,这个简单的方法是显示一个链接,如果用户点击它,就会弹出一个警报 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<!-- uncommenting the next line prevents the alert from showing up -->
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"> -->
<script type="text/javascript">
$( document ).ready(function() {
$("#show_more_link").click(function(e) {
alert("on click running");
e.preventDefault();
});
});
</script>
</head>
<body>
<a id="show_more_link" href="#">click here</a>
</body>
</html>
$(文档).ready(函数(){
$(“显示更多链接”)。单击(功能(e){
警报(“点击运行”);
e、 预防默认值();
});
});
问题:取消对包含引导程序的小型Javascript的行(第10行)的注释会中断onclick事件:
- 如果该行被注释掉,单击链接会弹出一个警报(如预期的)
- 如果该行未注释,则单击链接不会执行任何操作
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js">
您的脚本未关闭/缺少关闭标记,这就是为什么会出现错误和代码中断,您需要这样做:
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>