我正在开发 MVC 应用程序并使用 razor 语法。
在此应用程序中,我提供评论功能。
我用
show comments
段落。
<p id="ClassPara" class="ShowComments" onclick="chkToggle()">Show Comments</p>
现在,当用户单击“显示评论”时,我会切换 Div
,在该 div 中显示评论。
现在,我想将段落的文本从“显示评论”更改为“隐藏评论”, 当用户单击该段落时。
但我无法将文本更改为“隐藏评论”...它一直显示“显示评论” 怎么做 ?
我的代码是......
<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".ShowComments").click(function () {
$(".ParentBlock").slideToggle("slow");
if ($('.ShowComments').text = "Show Comments"
{
$('.ShowComments').text('Hide');
}
else
{
$('.ShowComments').text('Show Comments');
}
});
});
</script>
最佳答案
您可以使用如下所示的 .text()
方法的回调函数来简化代码。
$(document).ready(function() {
$(".ShowComments").click(function() {
$(".ParentBlock").slideToggle("slow");
// here instead of .ShowComments you should use
// this, where this refers to clicked element
$(this).text(function(i, oldText) {
return oldText == 'Show Comments' ? 'Hide' : 'Show Comments';
});
});
});
关于javascript - 如何使用 Jscript 更改 <p> 的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12435118/