2013-03-07 70 views
-1

http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js我的DIV沒有進行重定向

$(".myBox").click(function(){ 

window.location=$(this).attr("http://google.com"); 

return false; 
}); 

div width="200px" height="200px" class="myBox">ggg 
div 
+1

您沒有正確使用.attr()。 http://api.jquery.com/attr/ – BZink 2013-03-07 21:00:11

+1

你會問這個問題多少次http://stackoverflow.com/questions/15281189/my-div-is-not-redirecting-using-jquery – 2013-03-07 21:13:05

回答

0

這裏是一個工作示例

http://jsfiddle.net/axqCK/1/

$(函數(){

$(".myBox").click(function(){ 
     window.location="http://bing.com"; 
     return false; 
    }); 

});

請記住,某些網站(例如Google)不允許您在框架內加載。

0

假設有一個在div你點擊(我想不出一個有效的屬性的一個div元素包含href值,但不管)的屬性:

<!-- the following is an invalid use of a `href` attribute, please never do this --> 
<div class="myBox" href="http://google.com/">http://google.com/</div> 

$(".myBox").click(function(){ 
    var newURL = $(this).attr('href'), 
     newWindow = window.open(newURL, 'newWindowName'); 
    return false; 
}); 

如果您「再使用自定義data-*屬性(你應該,如果你這樣做):

<div class="myBox" data-href="http://google.com/">http://google.com/</div> 

$(".myBox").click(function(){ 
    var newURL = $(this).data('href'), 
     newWindow = window.open(newURL, 'newWindowName'); 
    return false; 
}); 

如果您使用的是的0​​文本:

<div class="myBox">http://google.com/</div> 

$(".myBox").click(function(){ 
    var newURL = $(this).text().trim(), 
     newWindow = window.open(newURL, 'newWindowName'); 
    return false; 
}); 

你的代碼是不工作的原因是這條線:

window.location = $(this).attr("http://google.com"); 

attr()是一個getter,setter方法或;到得到屬性的值:

window.location = $(this).attr('nameOfAttribute'); 

設置屬性的值:

window.location = $(this).attr('nameOfAttribute', 'valueOfAttribute'); 

你,在你的代碼,試圖檢索http://google.com屬性的值;不用說,它不存在。

引用:

+0

我設置了這個在這裏,它仍然沒有工作....看到源代碼在瀏覽器.... http://supersimplescripts.com/dom/test.htm – Greg 2013-03-07 21:21:30

+0

得到它的工作我的錯誤,但我們可以讓它在同一窗口中打開井 – Greg 2013-03-07 21:29:38

+0

它應該已經在同一個窗口中打開;這就是'window.location' *做的*。 – 2013-03-07 21:31:00