How can I make an entire div a link, but also have other links inside?

前端 未结 6 1221
广开言路
广开言路 2021-01-21 00:46

I have a div. I want it to link somewhere when you click on it, but I also want to put other links inside it, so when you click on the other links you\'re redirected to one plac

6条回答
  •  北海茫月
    2021-01-21 00:58

    $(".exterior a").click(function(e){
        alert('a clicked but div not triggered');
        e.stopPropagation();
    });
    
    $(".exterior").click(function(e){
        alert("div clicked but not a");
    })
    
    
    
    .exterior{
    width: 500px;
    height: 100px;
    background-color: red;
    }
    

    I used stop propagation on a element to prevent it from triggering the click on the div. And i used div as wrapper so you would have to put a windows.location if you want to redirect to an url inside the click function.

    I'm not sure how you can achieve this with simply html and css. So i would suggest using jquery.

提交回复
热议问题