我试图点击#ID并打开一个网址 - 但[作为新手] - 我似乎无法得到它。我正在使用
$('#Test').click(function() {
OpenUrl('some url');
return false;
});
答案 0 :(得分:3)
类似的东西:
$("#Test").click(function(event){
window.location.href = "some url";
event.preventDefault();
});
答案 1 :(得分:2)
只需使用window.location = 'some url'
$('#Test').click(function() {
window.location = 'http://www.google.com'
return false;
});
为了详细说明,window.location
是一个具有不同非常有趣属性的对象,您可以read more about it here。简而言之,它包含以下属性(引自链接):
Property Description Example
hash the part of the URL that follows the #test
# symbol, including the # symbol.
host the host name and port number. [www.google.com]:80
hostname the host name (without the port number www.google.com
or square brackets).
href the entire URL. http://[www.google.com]:80
/search?q=devmo#test
pathname the path (relative to the host). /search
port the port number of the URL. 80
protocol the protocol of the URL. http:
search the part of the URL that follows the ?q=devmo
? symbol, including the ? symbol.
由于window.location
是一个对象,它也可以包含window.location
所做的方法。通过使用这些方法,您可以更好地控制页面的加载方式,即强制从服务器重新加载或允许浏览器使用缓存条目,跳过创建新的历史记录点,而不是仅仅为对象分配字符串。等
以下是可用方法的概述:
Method Description
assign(url) Load the document at the provided URL.
reload(forceget) Reload the document from the current URL. forceget is a
boolean, which, when it is true, causes the page to always
be reloaded from the server. If it is false or not specified,
the browser may reload the page from its cache.
replace(url) Replace the current document with the one at the provided
URL. The difference from the assign() method is that after
using replace() the current page will not be saved in
session history, meaning the user won't be able to use
the Back button to navigate to it.
toString() Returns the string representation of the Location object's
URL.
如果您愿意,也可以在新窗口中打开资源。请注意,有些用户不喜欢在新窗口中为他们打开链接,并且更愿意自己有意识地做出这个决定。但是,您可以做的是在Click-handler中模仿某些功能,并尝试找出单击了哪个鼠标按钮。如果是鼠标中键,那么大多数浏览器都会在新窗口中打开链接。这将不完全相同,因为用户将无法右键单击并选择“在新窗口中打开”,但它可能已经足够了。无论如何,这里是如何在新窗口中打开资源:
var WindowObjectReference;
function openRequestedPopup()
{
WindowObjectReference = window.open(
"http://www.domainname.ext/path/ImageFile.png",
"DescriptiveWindowName",
"resizable=yes,scrollbars=yes,status=yes");
}
答案 2 :(得分:1)
如果您的OpenUrl函数看起来像这样,这应该可以正常工作:D
function OpenUrl(url){
window.location = url;
}
btw:为什么在点击时返回false?