我试图在切换元素时将“+”更改为“-”。我已经尝试了一些方法,但是一旦符号发生变化,它就不会像应有的那样来回切换。我搜索了其他帖子,但它们不适用于我的代码。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
//Hide (Collapse) the toggle containers on load
$("trace_dump").hide();
//Slide up and down on hover
$("h3").click(function(){
$(this).next("trace_dump").slideToggle("slow");
$(this).text($('span').text() == '+' ? '-' : '+');
});
});
</script>
...这是 html:
<h3><span>+</span>print_r()</h3>
<trace_dump>
print_r($e);
</trace_dump>
<h3><span>+</span>var_dump()</h3>
<trace_dump>
var_dump($e);
</trace_dump>
我可以得到一些想法吗?
最佳答案
在这一行中:
$(this).text($('span').text() == '+' ? '-' : '+');
this
指的是 h3,而不是我认为您想要的 span
。
试试这个:
$(this).find('span').text($(this).find('span').text()=='+'?'-':'+');
关于javascript - 点击切换元素时文本会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5066933/