我喜欢一个 h3 标题,上面有一个小字符,上面写着“展开我”
它看起来像这样:类别 xyz [+]
我喜欢 jquery 中的一个函数,可以在单击时将 + 更改/删除为减号。我知道如何将文本附加到字符串,但不知道如何删除它或只是删除其中一部分。
如果您有更好的方法请随时告诉我..谢谢!
<小时/>注意:实现 .remplace 函数...但没有按预期工作 you can see it here ...问题我使用 jquery 1.4 切换到 1.4.3 现在它可以工作了!
最佳答案
只需切换现有元素的可见性就容易得多。
HTML
<h3>Category XYZ
<span id="toggle">
<a href="#" class="expand" title="expand me">[+]</a>
<a href="#" class="hide" title="hide me">[-]</a>
</span>
</h3>
<div id="toggleContent">This is the text of category XYZ</div>
Javascript
$("span#toggle a").click(function() {
$("div#toggleContent").toggle();
$("span#toggle a").toggle();
});
CSS
div#toggleContent { display: none; }
span#toggle a.hide { display: none; }
<强> JSFiddle 你可以尝试一下。
关于jquery 追加和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4129221/