2017-05-01 33 views
1

我有一個<ul><li><a>結構,非常具體風格化,並有9個元素分爲3列。我想添加另一個項目而不創建第四列,因此我正在考慮的解決方案是在一個「插槽」中有2個鏈接。2鏈接在一個錨

問題在於<ul><li><a>的結構使得添加一個<a>將自動創建另一個槽(垂直)。

爲了適應這種變化,我希望不必重新創建整個結構,所以我試圖想出一個黑客攻擊。

我知道這是醜陋和怪異,但它確實使爲atleast一個有趣的問題......

我試圖做:

<a href="link1.html">link1 <span onclick="location.href ='link2.html'">link2</span></a> 

但是,這並不工作 - 我也試過用jQuery但那也沒用。這裏是否存在解決方法/破解或我必須重構整個列表?

+1

你能舉一個你正在努力實現的完整例子嗎? – empiric

+2

您需要[停止傳播](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Examples#Example_5:_Event_Propagation)點擊到父錨元素。不幸的是,使用'onclick'屬性沒有簡單的方法。 – Blazemonger

+0

@Blazemonger是的。 ''並在函數內部調用'event.stopPropagation()'。 – mhodges

回答

0

$('#link2').click(function() { 
 
    window.setTimeout(function() { 
 
    location.href = $('#link2').attr('data-href') 
 
    }, 10) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="link1.html">link1 <span id='link2' data-href="link2.html">link2</span></a>

+0

您應該使用準確表示原始問題的代碼。 – Blazemonger

+0

@Blazemonger當你的編輯未經我的同意而更新時,它確實很奇怪:) – Ninjaneer

+0

可能是我錯了。我以爲他想要兩個hrefs ..這就是爲什麼他加了一個額外的'span'。他可以添加它作爲另一個屬性,可能是'href2'到相同的'a'標籤 – Ninjaneer

1

這裏有一個快速簡便的方法有aspan元素帶你到不同的鏈接。 (I註釋掉實際鏈路和使用警報來代替,因爲鏈接不會在幀加載反正。)

function goToLink(e, that) { 
 
    e.stopPropagation(); 
 
    if (that.tagName.toUpperCase() == "A") { 
 
    // if anchor was clicked 
 
    alert("1st link"); 
 
    //window.location.href = "http://www.google.com"; 
 
    } 
 
    else { 
 
    // if span was clicked 
 
    alert("2nd link"); 
 
    //window.location.href = "http://www.yahoo.com"; 
 
    
 
    } 
 
}
<a href="#" onclick="goToLink(event, this);">link1 <span onclick="goToLink(event, this);">link2</span></a>

stopPropagation()呼叫阻止加載當第二第一鏈路鏈接被點擊。