2015-06-19 33 views
1

我有一個div(A),並在另一個div(B)內,然後我有我的鏈接。我想要發生的是,當我們點擊A時,div(B)中的鏈接起作用。jQuery鏈接工程時點擊父母的div

我成功由一個家長這樣做,但不是2 ...

$(".block").mouseover(function() { 
 
    if ($(".block .button").length) { 
 
     $(this).css("cursor", "pointer").find(".invisible.button").css("text-decoration", "none"); 
 
    } 
 
    }).mouseout(function(e) { 
 
    e.preventDefault(); 
 
    e.stopPropagation(); 
 
    $(this).find(".invisible.button").css("text-decoration", "none"); 
 
    }).click(function(e) { 
 
    document.location.href = $(this).find(".invisible.button").attr("href"); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
 
<div class="wrap"> 
 
    <div class="wrap-in block"> 
 
    <p>content</p> 
 
    <a class="invisible button" href="mylink"> go</a> 
 
    </div> 
 
</div>

我找不到讓我的鏈接方式工作的」單擊時。包裹」分區,如果有人可以幫助:)

一個信息 如果‘包裝’有沒有聯繫,我還有一個類:

的一點是,如果我只是做:

$(".wrap").mouseover(function() { 
    if ($(".block .button").length) { 
     $(this).css("cursor", "pointer").find(".invisible.button").css("text-decoration", "none"); 
    } 
    }).mouseout(function(e) { 
    e.preventDefault(); 
    e.stopPropagation(); 
    $(this).find(".invisible.button").css("text-decoration", "none"); 
    }).click(function(e) { 
    document.location.href = $(this).find(".invisible.button").attr("href"); 
    }); 

它的工作原理,但它工作在DIV 沒有 anylink太...

和我的代碼,中止「nolink」

$('.nolink').on('click', function(e){ 
     e.preventDefault(); 
     e.stopImmediatePropagation(); 
    }); 
+1

不清楚你問:( –

+0

點擊'#wrap'觸發點擊'.invisible' ?鏈接,然後 – Drakes

+0

'$( 「#換行」)上( 「點擊」,功能(E){$( 「a.invisible」)觸發( 「點擊」);});' – Drakes

回答

0

您是否希望做一些像這樣的:

$(".wrap").on("click", function() { 
    window.location = $(this).children("div.wrap-in").children("a.invisible").attr("href"); 
}); 
+0

我建議改變這兩個.children()函數只是一個「查找」。在這種情況下,使用「.children()」獲得的效果並不明顯,所以你可能會讓代碼更具可讀性:) – NachoDawg

+0

我絕對同意。我在與您的評論完全相同的時間發佈了我的答案。我認爲它看起來更乾淨,但我的答案已經足夠,所以我離開了它。同樣,你的答案和.find方法會更清晰和更易於閱讀,但不會使我的答案錯誤。 – NightOwlPrgmr

+0

同意,你的回答可以解決問題 – NachoDawg

1

如果錨是永久隱藏的,有幾個解決方案會更「正確」。

就像將包裝轉化爲錨。如果容器總是會導致頁面重定向,那麼爲什麼不讓整個div成爲一個鏈接呢?

此外,您還可以隱藏在捲紙上出現數據標記中的href ADRESS。

<div class="wrapper" data-href="myUrl">(...)</div> 


$(".wrap").on("click", function() { 
    window.location.replace($(this).data("href")); 
}); 
+1

好方法 - 投了票。 – NightOwlPrgmr

0

好的,非常感謝您的幫助!我找到了一種方法去做我真正想做的事情。因爲我需要在包含鏈接的div上添加鏈接和光標。

這是我的代碼清潔(我希望),和它的作品,沒有undifined鏈接

$(".wrap").mouseover(function(){ 
     if($(this).find("a.invisible").length){ 
      $(this).css("cursor","pointer"); 
     }  
    }).mouseout(function(e){ 
     $(this).css("cursor","auto"); 
    });  


    $(".wrap").on("click", function() { 
     if($(this).find("a.invisible").length){ 
      btn = $(this).find("a.invisible"); 
      window.location = $(this).find("a.invisible").attr("href"); 
     } 
    });