1

知道这里,我用 Jquery 和 CSS 制作了一个无文本的、被阻止的、可点击的 div。单击后,我想将一个新 URL 加载到浏览器中,从而将我的访问者从我的网站上转移到 stackoverflow.com。你能用 Jquery 做到这一点吗?如果有怎么办?

 #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>

第二个问题我必须让 div 透明或为空,以便显示菜单背景(没有切片。)。我可以或应该使用透明的 gif 来做到这一点吗?

顺便说一句:我如何修改本地 URL 的代码?谢谢!

4

1 回答 1

3

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';
});
于 2012-06-30T20:19:11.630 回答