我使用 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/