[英]How to toggle the anchor text using Jquery?
這是我的HTML代碼:
<label for="LoginControl">
<a href="login/" class="concealed noOutline"> Log in or Sign up </a>
</label>
我需要在單擊時替換文本登錄或使用X注冊,並在再次單擊時返回舊文本。
你需要一個錨的點擊處理程序:
$("a.concealed").click(function() {
現在你需要一些邏輯:
$(this).text() == "X" ? "Log in or Sign up" : "X"; //if it equals X, return Log. Else X.
現在只需組合成一個函數:
$("a.concealed").click(function() {
var newText = $(this).text() == "X" ? "Log in or Sign up" : "X";
$(this).text(newText);
});
演示: http : //jsfiddle.net/afuub/
如果你給你的標簽一個id,那將是最好的
<label for="LoginControl"><a id="myLink" href="login/" class="concealed noOutline">Log in or Sign up</a></label>
然后你就可以這樣做了: 小提琴
HTML
<label for="LoginControl">
<a href="login/" class="concealed noOutline"> Log in or Sign up </a>
</label>
JQUERY
$('label a').click(function(
$(this).toggle(
function(){$(this).text('X')},
function(){$(this).text('Login or Sign Up')}
)}
閱讀更多關於Jquery Toggle http://api.jquery.com/toggle/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.