gpt4 book ai didi

javascript - jQuery 和 Bootstrap 3 : TypeError: "Object [object HTMLAnchorElement] has no method ' preventDefault'

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:52 25 4
gpt4 key购买 nike

我正在 Chrome 中处理一个包含两个 Bootstrap 3 模态的页面。在您打开开发人员控制台并启动其中一个模式之前,一切看起来都很好。你可以用这个 jsfiddle 重复这个问题,再用最新的 Chrome,或者用下面的代码创建一个 html 页面。

http://jsfiddle.net/ow3n/22ENG/

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type="text/javascript">

function launchReportModal(){
$('#reportModal').modal();
}
function launchShareModal(){
$('#shareModal').modal();
}
</script>
</head>
<body>

<a href='#reportModal' onclick='launchReportModal()' class='report_link' data-toggle='modal' data-target='#reportModal'>report</a>

<a href='#shareModal' onclick='launchShareModal()' class='report_link' data-toggle='modal' data-target='#shareModal'>share</a>

<div class="modal fade" id="reportModal" tabindex="-1" role="dialog" aria-labelledby="reportModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">Report <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button></div>
<div class="modal-body"></div>
<div class="modal-footer"></div>
</div>
</div>
</div>

<div class="modal fade" id="shareModal" tabindex="-1" role="dialog" aria-labelledby="shareModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">Share <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button></div>
<div class="modal-body"></div>
<div class="modal-footer"></div>
</div>
</div>
</div>
</body>
</html>

编辑:使用 jQuery 选择器在 html 中启动模式修复了这个问题,但无论如何您仍然可以在 jsfiddle 中看到这个问题。

$('#launchReportModal').on('click', function(){
$('#reportModal').modal();
});
$('#launchShareModal').on('click', function(){
$('#shareModal').modal();
});
<a href='#' id='launchReportModal' data-toggle='modal' data-target='#reportModal'>report</a>

<a href='#' id='launchShareModal' data-toggle='modal' data-target='#shareModal'>share</a>

最佳答案

您正在尝试自动激活模态框同时手动激活它们!如果您阅读 docs :

Activate a modal without writing JavaScript. Set data-toggle="modal" on a controller element, like a button, along with a data-target="#foo" or href="#foo" to target a specific modal to toggle.

两者都有

<a href='#' id='launchReportModal' data-toggle='modal' data-target='#reportModal'>report</a>

$('#launchReportModal').on('click', function(){
$('#reportModal').modal();
});

错误是这样触发的:

$.fn.modal (_relatedTarget)
Modal.toggle (_relatedTarget)
Modal.hide (_relatedTarget)

_relatedTarget 在代码中被视为事件:

Modal.prototype.hide = function (e) {
if (e) e.preventDefault()
-> Uncaught TypeError: Object http://fiddle.jshell.net/22ENG/2/show/# has no method 'preventDefault'

但是 _relatedTarget 不是一个事件,它是一个元素。显然 data-target 的排名高于您的 $(element).modal() 代码。

解决方案:只需跳过模式的 javascript 激活 -> http://jsfiddle.net/QFyv9/ - 使用未缩小版本的 3.1.1 bootstrap.js fork jsFiddle(这样我就可以找到问题所在)

关于javascript - jQuery 和 Bootstrap 3 : TypeError: "Object [object HTMLAnchorElement] has no method ' preventDefault',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22597349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com