[英]How to trigger javascript when a page is loaded to a specific div ID?
I have the following div: 我有以下div:
<div id="tutorial" onclick="console.log('Triggered tutorial');"></div>
I want to target this div via the URL, by navigating like this: 我想通过以下方式通过网址定位此div:
mysite.com/mypage.html#tutorial
However, loading the page to this div ID doesn't trigger the onclick event. 但是,将页面加载到该div ID不会触发onclick事件。
What is the best way to accomplish this? 做到这一点的最佳方法是什么?
Use onload 使用onload
<div id="tutorial" onload="onLoadTutorial();"></div>
function onLoadTutorial() {
if (document.location.endsWith("#tutorial")) {
//...
}
}
Instead of calling hashChanged(), call your own onclick function or execute the content inside of it 而不是调用hashChanged(),而是调用自己的onclick函数或在其中执行内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.