基于带有#的url打开模态
Open modal based on url with #
当右片段在URL中时,我有两个模式要显示,因此domain.com#steg2
加载model#steg2,domain.com#steg3
加载model#steg3。
我的脚本:
$(function() {
if (window.location.hash.indexOf("steg2") !== -1) {
$("#steg2").modal();
}
if (window.location.hash.indexOf("steg3") !== -1) {
$("#steg3").modal();
}
});
我的模态就是这样构建的:
<div class="modal fade" id="steg2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal fade" id="steg3" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
两个模式都在同一个页面/URL上。我做错了什么?站点正在使用bootstrap 3和jQuery。
经过一些尝试,我发现了以下代码来完成工作:
<script type="text/javascript">
jQuery(document).ready(function($) {
var target = document.location.hash.replace("#", "");
if (target.length) {
if(target=="steg1"){
$('#steg1').modal('show');
}
else if(target=="steg2"){
$('#steg2').modal('show');
}
else if(target=="steg3"){
$('#steg3').modal('show');
}
}
else{
}
});
</script>
我建议:
function hashModal () {
var stegs = ['steg2','steg3'],
hash = window.location.hash,
stegAt = stegs.indexOf(hash.replace('#','');
if (stegAt > -1) {
$(hash + '.modal').modal();
}
}
$(document).on('ready hashchange', hashModal);
参考文献:
- JavaScript:
Array.prototype.indexOf()
window.location
- jQuery:
on()
首先使用window.location.hash
从url中获取哈希。这个值前面会加一个#。然后你可以直接把它用作jquery选择器。
var hash = window.location.hash;
if(hash != '') {
$(hash).modal();
}
相关文章:
- 我想使用模态通过php文件发送邮件,并且我希望在提交关闭后关闭pop
- 打开一个模态并将其链接到AngularJS中的指令
- SemanticUI模态not onDeny/onApprove事件未激发
- jqueryajax调用是复制url,当在模态视图之后在外部js文件中进行调用时
- 如何从url中显示模态内部的信息
- 打开一个模态(在Codeigniter中),该模态从依赖于phpvar的URL加载(在iframe中)内容
- 基于带有#的url打开模态
- 先前的状态视图不会保留在带有自定义模态url的打开UI引导模态的后台
- 使用SP.UI.ModalDialog.ShowPopupDialog(url)打开对话框时,模态弹出窗口发送值给父窗口
- 恢复基础url后模态窗口
- 如何在模态对话框中显示URL和URL标题
- AngularJS ui-bootstrap打开模态窗口而不改变url
- url的模态对话框不工作
- 任何URL的Colorbox模态
- 模态窗口内的Url重定向
- 将按钮值传递给Url.模态中的动作
- 在模态加载上更改url
- 如何获得"这个“;背景图像's的url使用“$event.target”;以离子模态显示
- 如何在模态中加载 URL 的内容并在浏览器中更改地址
- 带有URL的Javascript模态弹出窗口