简体   繁体   中英

jquery how to make a clickable div open in a new window

I want to make a DIV with no content clickable for that I use this jquery code:

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
$("#whole").click(function () {
    window.location = $(this).attr("href");
    });
});

</script>

It works fine, but the link always opens in the same window. How do I need to change the code, to make the div open in a new window?

$("#whole").click(function () {
   window.open($(this).attr("href"), '_blank');
});

你需要window.open

window.open($(this).attr("href"))
$(document).ready(function() {
$("#whole").click(function () {
    window.open("href"); // window.open('https://stackoverflow.com/questions/13452398/jquery-how-to-make-a-clickable-div-open-in-a-new-window');
    });
});

You can do by following method

1)

$(document).ready(function() {
$("#divId").click(function () {
window.open("openThisPage.html"); //Change 'openThisPage.html' to your target page
});
});

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