[英]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.