我想在单击以显示文本区域时将链接"添加其他注释",并将文本切换为"删除注释".单击"删除注释"时,它应隐藏文本区域,文本需要更改为"添加其他注释".
我用的代码是
<script type="text/javascript">
$(document).ready(function( ){
$("#addcmt").click(function( )
{
$(".commentarea").toggle( );
if ($("#addcmt").text = "Add additional comment") {
$("#addcmt").text("Remove comment");
}
else {
$("#addcmt").text("Add additional comment");
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
html是
<div class="addlcomment">
<a id="addcmt">Add additional comment</a>
</div>
<div class="commentarea" style="display:none;">
<textarea name="strcomments1" tabindex="2"></textarea>
</div>
Run Code Online (Sandbox Code Playgroud)
文本切换不起作用.
任何帮助表示赞赏.
工作演示 http://jsfiddle.net/eDNH5/10/
这会对你有所帮助!
问题是你使用赋值运算符而不是相等 ==
Jquery代码
$(document).ready(function() {
$("#addcmt").click(function() {
$(".commentarea").toggle();
if ($("#addcmt").text() == "Add additional comment") {
$("#addcmt").text("Remove comment");
}
else {
$("#addcmt").text("Add additional comment");
}
});
});?
Run Code Online (Sandbox Code Playgroud)
如果要检查块是否可见以更改文本:
$(document).ready(function() {
$("#addcmt").click(function() {
var isVisible = $(".commentarea").toggle().is(":visible");
$(this).text( isVisible ? "Remove comment" : "Add additional comment");
});
});
Run Code Online (Sandbox Code Playgroud)
我正在使用这个,因为你已经查找了#addcmt一次,无需真正再次找到该元素.