简体   繁体   中英

javascript click event not working in div onclick

I have this div in my page that is showing X at top of modal, I like to by clicking this user redirect to homepage. I tried this but the even is not firing at all.

  <div class="modal-content">
    <div class="close" onclick="redirecttologin()">x</div>
......
  </div

this is java script but even the alert is not showing

 function redirecttologin() {
  alert("test");
document.location.href = "/";
 };

I am sure that it has the reference to java script file as other things are working in page.

*problem was class="close".

Your code should work properly, the only issue is that you should use window.location instead of document.location . They are both the same, but as documentation says, the document.location is read only property.

Also notice, that your closing <div> tag is missing > . That might be a problem as well.

 function redirecttologin() { alert("test"); window.location.href = "/"; };
 <div class="modal-content"> <div class="close" onclick="redirecttologin()">x</div> </div>

你必须使用

window.location.href = "/"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM