2013-05-21 19 views
0

我想要使用單擊鏈接顯示在div中的名稱。我嘗試了很多東西,但沒有出現。我也有一個小腳本來顯示和隱藏一個工作正常的div,直到我試圖爲該名稱創建一個腳本。 有人請快速看看。獲取鏈接名稱並將其寫入div中

//script for hiding and display div 
function showDiv() { 
    document.getElementById('popup').style.display = "block"; 
} 
function hideDiv() { 
    document.getElementById('popup').style.display = "none"; 
} 

和HTML:

<div class="maintab"> 
    <div class="tab" onclick="showDiv()" ><a href="#">Template</a></div> 
</div> 
<div id="bestelknop">**Here the name must come**</div> 
<div id="popup" style="display:none"> 
    <ul> 
     <a href="http://pixelweb.be" target="iframe" onclick="hideDiv()" name="Pixelweb">pixelweb</a> 
     <a href="http://templates.pixelweb.be/social" target="iframe" onclick="hideDiv()" name="Social">social</a> 
     <a href="http://templates.pixelweb.be" target="iframe" onclick="hideDiv()" name="Templates" >templates pixelweb</a> 
    </ul> 
</div> 

現在我想顯示在div bestelknop名範·德·當前鏈接。

我是javascript中的newbee,所以請幫助我。

問候,

+0

顯示元素,設置*將style.display *爲「」(空字符串),使得元件採用其繼承或缺省值。 – RobG

回答

2

您可以通過當前的鏈接功能。

看看我的工作示例這裏:http://jsfiddle.net/XfW3P/

<div class="maintab"> 
    <div class="tab" onclick="showDiv()" ><a href="#">Template</a></div> 
</div> 
<div id="bestelknop">**Here the name must come**</div> 
<div id="popup" style="display:none"> 
    <ul> 
     <a href="http://pixelweb.be" target="iframe" onclick="hideDiv(this)" name="Pixelweb">pixelweb</a> 
     <a href="http://templates.pixelweb.be/social" target="iframe" onclick="hideDiv(this)" name="Social">social</a> 
     <a href="http://templates.pixelweb.be" target="iframe" onclick="hideDiv(this)" name="Templates" >templates pixelweb</a> 
    </ul> 
</div> 

的JavaScript代碼:

function showDiv() { 
    document.getElementById('popup').style.display = "block"; 
} 
function hideDiv(link) { 
    document.getElementById('popup').style.display = "none"; 
    document.getElementById('bestelknop').innerHTML = link.name; 
} 
+1

不錯,和我有同樣的想法。 +1 – tymeJV

+0

嗨,thanx的答覆,但它可以是不會工作,因爲不是整個頁面再次加載只有IFRAME?我也說過它錯了,它最顯示在標籤中,但這不可能。 –

+0

它應該沒有頁面重新加載。 – iurisilvio

2

首先,通過this到你的`的onclick = 「hideDiv(本)」,在每一個環節:

<a href="http://pixelweb.be" target="iframe" onclick="hideDiv(this)" name="Pixelweb">pixelweb</a> 
<a href="http://templates.pixelweb.be/social" target="iframe" onclick="hideDiv(this)" name="Social">social</a> 
<a href="http://templates.pixelweb.be" target="iframe" onclick="hideDiv(this)" name="Templates" >templates pixelweb</a> 

然後,改變你的hideDiv()功能:

function hideDiv(obj) { 
    document.getElementById('popup').style.display = "none"; 
    document.getElementById('bestelknop').innerHTML = obj.name + " " + obj.href; 
} 

小提琴:http://jsfiddle.net/bUVtA/

+0

如果我做了更改hideDiv不會工作的annymore。我輸入錯誤,輸出必須顯示在div bestelknop中。它最常進入div選項卡。但這不能成爲這個問題。 –

相關問題