简体   繁体   中英

Jquery find link in Div - open as defined in html

I need some div's to work as a clickable link. I already found the solution for jquery. Unfortunately this solution is always window.location or window.open. This is not suitable for us, since we have a lot of div's and the urls are already defined with _blank or same window.

I have to admit, that I am not sure, how I can look for the function that I need, since I have almost no knowledge of Javascript and the functions of it.

This is the code for the script, that I found:

$(document).ready( function () {
$(".textlink").click(function () {
        window.location = $(this).find("a:first").attr("href");
        return false;
    });  
});

and this is one box with target=_blank

<div class="textlink texticon texticon-top"><a href="http://term1caq/WebCAQ.Net/App.QBD/DocumentView.aspx?id=429" target="_blank">Text in DIV</a></div>

The expected result would be, that the whole div is clickable but the target will be taken from the href of the div and not predefined in the script.

You just need to trigger click on found anchor tag instead of setting its href to window.location

$(document).ready( function () {
    $(".textlink").click(function () {
        $(this).find("a:first").click();
    });  
});

You can do:

 $('.textlink a:first').click(function() { window.location.replace($(this).attr('href')); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="textlink texticon texticon-top"><a href="http://term1caq/WebCAQ.Net/App.QBD/DocumentView.aspx?id=429" target="_blank">Text in DIV</a></div>

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