简体   繁体   中英

Jquery: div Hyperlink

knewb here, I have made a textless, blocked, div clickable with Jquery and CSS. Upon click I would like to load a new URL into the browser thus taking my visitor away from my website to say stackoverflow.com. Can you do that with Jquery? If so how?

 #star{
    width:130px;
    height:40px;
    outline:1px solid orange;
    display:block;
    cursor:pointer;
    }

<div id="star">star</div>

<script>    
    $("#star").click(function(evt){
        $(this).html("http://www.stackoverflow.com");
    });
</script>

Second question I have to have the div transparent or empty so the menu background shows, (no slicing.). Can I or and should I do this with a transparent gif?

BTW: also how do I modify the code for a local URL? Thank you!

The first part is relatively easy:

$("#star").click(function(evt){
    window.location = 'http://stackoverflow.com';
});

As for transparency, simply add the following to your CSS:

#star {
    /* other stuff */
    background-color: transparent;
}

Or, if you don't necessarily need full cross-browser compatibility:

#star {
    /* other stuff */
    background-color: rgba(255,255,255,0.1);
}

The above will make the element's background-color white, with an alpha transparency of 0.1 ( 0 being fully transparent, 1 being fully opaque).

Note, I wasn't quite sure what you meant by 'local url,' but if you mean is it possible to use a relative path, for example to change:

'http://server.com/news/index.html'

To:

'http://server.com/some/other/directory/index.html'

without using an absolute path in the JavaScript, then the following should work:

$("#star").click(function(evt){
    window.location.pathname = '/somewhere_else/on/the/same/server';
});

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