简体   繁体   English

Jquery:div Hyperlink

[英]Jquery: div Hyperlink

knewb here, I have made a textless, blocked, div clickable with Jquery and CSS. 在这里知道,我用Jquery和CSS创建了一个无文本,被阻止,可点击的div。 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. 点击后我想在浏览器中加载一个新的URL,从而让我的访问者离开我的网站说stackoverflow.com。 Can you do that with Jquery? 你能用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.). 第二个问题我必须让div透明或空,所以菜单背景显示,(没有切片。)。 Can I or and should I do this with a transparent gif? 我可以和我应该用透明的GIF做到这一点吗?

BTW: also how do I modify the code for a local URL? 顺便说一句:我如何修改本地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: 至于透明度,只需将以下内容添加到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). 以上将使元素的background-color白色,alpha透明度为0.10表示完全透明, 1表示完全不透明)。

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: 如果不在JavaScript中使用绝对路径,则以下内容应该有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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