2011-09-29 309 views
0

標籤內更改文字我已經定義的標籤,其中在鼠標懸停我改變鼠標懸停

<a class="bid-addwatchlist add-logout" href="https://stackoverflow.com/users/login" onmouseover="changeText(this)" onmouseout="sameText(this)"><span style="font-size: 32px; line-height: 18px;">+</span><span style="font-size: 14px; float: left;">Add to watchlists</span></a> 

<script> 
    function changeText(theTag){ 
     theTag.innerHTML = '<span style="font-size: 1.7em; text-align:center; line-height:50px;">Login</span>'; 
    } 

    function sameText(theText){ 
     theText.innerHTML = '<span style="font-size: 32px; line-height: 18px;">+</span><span style="font-size: 14px; float: left;">Add to watchlists</span>'; 
    } 
</script> 

我已經給下面的CSS這樣做的一個標籤中的文本標籤,使它看起來像一箱

.bid-addwatchlist{ 
    background-color : #32679B; 
    border-radius : 5px 5px 5px 5px; 
    color : #FFFFFF; 
    cursor : pointer; 
    float : left; 
    height : 56px; 
    line-height : 14px; 
    padding-top : 4px; 
    text-decoration : none; 
    width : 76px; 
    margin-left : 11px; 
} 

它工作正常,但我面臨的問題是,當我做鼠標懸停文本更改登錄,如果我點擊登錄文字什麼也沒有發生,而如果我在其他側面區域單擊它是加工。我不明白爲什麼會發生這種情況,我現在需要做的是如果我點擊登錄文本,它的工作原理。請提出我需要做的或這是因爲登錄文本不在a標籤內。我所需要做的是我想更改標籤onmouseover中的文本,因此它應該工作。

我也試過,在跨度定義click事件的文字,但沒有工作

function changeText(theTag){ 
     theTag.innerHTML = '<span style="font-size: 1.7em; text-align:center; line-height:50px;" onclick=\'window.location="https://stackoverflow.com/users/login"\'>Login</span>'; 
    } 

注:文本僅僅是一個正常的鏈接

+0

對於'onclick'事件,您正在使用的代碼是什麼? –

+0

我還沒有定義任何點擊事件,因爲它只是一個普通的標籤,它會自動打開 – user850234

+0

你應該把它放到一個JSFiddle中,而不是隻顯示我們一些代碼。這會讓你的問題更容易回答,因爲我們可以使用你的代碼測試不同的方法 –

回答

0

我覺得你的問題是因爲它不在DOM解析的時間內,因此你爲onMouseOver放置的文本未綁定到鏈接元素。因此,我會將鏈接添加到登錄文本。

function changeText(theTag){ 
    theTag.innerHTML = '<span style="font-size: 1.7em; text-align:center; line-height:50px;"><a href="https://stackoverflow.com/users/login">Login</a></span>'; 
} 
+0

你有我的問題,但這不工作,現在試了 – user850234

+0

你確定嗎?它適用於我在LOGIN文本週圍額外使用,我可以單擊登錄按鈕並轉到/ users/login,只需CSS將需要一些工作,因爲它具有默認的醜陋藍色懸停和下劃線。 –

+0

它沒有工作在我的一個附加一個標籤 – user850234