在url中使用hashtag触发jquery单击元素
我想知道是否可以通过将用户发送到一个特定的url来触发弹出窗口,该url的末尾包含一个“#somerandomword”在url中使用hashtag触发jquery单击元素,jquery,Jquery,我想知道是否可以通过将用户发送到一个特定的url来触发弹出窗口,该url的末尾包含一个“#somerandomword” 所以通常他们点击页面上的一个按钮来触发弹出窗口,而我只希望当用户得到一个特定链接时触发弹出窗口 这可以在页面加载时轻松完成,只需在URL中查找哈希: $(document).ready(function(){ // the #-prefixed portion of the URL that serves // as a fragment identifie
所以通常他们点击页面上的一个按钮来触发弹出窗口,而我只希望当用户得到一个特定链接时触发弹出窗口 这可以在页面加载时轻松完成,只需在URL中查找哈希:
$(document).ready(function(){
// the #-prefixed portion of the URL that serves
// as a fragment identifier for the document, from
// www.example.com/page#fragment
// document.location.hash would return #fragment
let hash = document.location.hash;
// because the hash identifies a fragment of
// the document by its id, and the '#' character
// specifies an id-selector in CSS we pass that
// value to jQuery to select the correct element
// and call the click() method - without arguments -
// to trigger a click event:
$(hash).click();
});
或者,:target
的CSS选择器选择散列所针对的元素,因此也可以使用:
$(document).ready(function(){
$(':target').click();
});
参考资料:
- CSS:
- JavaScript:
- jQuery:
id
s上触发事件。否则:是的,事情就是这样。