javascript - 如何使用哈希值使用 jquery 打开 Bootstrap 模式?

标签 javascript jquery twitter-bootstrap bootstrap-modal

我使用 jquery 从 URL 获取哈希值,然后根据该值打开一个模式。到目前为止我的代码是:

$(function () {
   var activeModal = $('\$\(\'' + location.hash +'\'\)');
   console.log(activeModal);
   activeModal.modal('show');
});

但是当我运行这个时,我收到一个错误:jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: $('#modal-name') .

最佳答案

如果您的 location.hash 是(例如)#somehash,那么您需要使用以下方式获取该元素:

$('#somehash')

那么为什么你需要里面的'\$\(\''
您可以只使用:

var activeModal = $(location.hash);

你的代码所做的基本上是

 var activeModal = $("$('#modal-name')")

这不是你想要的。

关于javascript - 如何使用哈希值使用 jquery 打开 Bootstrap 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38957713/

相关文章:

javascript - 异步和 promise 从快捷方式更改

javascript - 如何在 TestCafe 中的测试中共享测试文件之间的全局变量?

javascript - 如何处理 PhantomJS 中的 `console.log.bind(console)` 抛出错误?

javascript - 函数调用不适用于 'click' 事件处理程序

javascript - 纯CSS关闭按钮

html - 用于 col-sm 的 Bootstrap 表调整大小/重组

javascript - 动画 Bootstrap 卡替换消失的卡

javascript - XMLHttpRequest 在 Chrome 扩展中修改了 Accept header

javascript - 在 Safari 中使用 input-group-addon 时出现 Bootstrap 工具提示故障

javascript - 如何将绑定(bind)值(asp.net控件)传递给jquery