HTML:
<tr class="headrow">
<th colspan="3"><span id="bigspan">-</span> Main Header </th>
</tr>
JQuery的:
$('tr.headrow').click(function () {
// Find the span inside the headrow and toggle its value to '+'
$(this).find('span').text(<<HOW?>>);
});
<<HOW?>>>
现在我需要在jQuery文本函数中添加一些东西,通过它我可以切换所选元素的文本val(这里是span)。我基本上想要改变它&#39; +&#39;,如果它的当前值是&#39; - &#39;反之亦然
答案 0 :(得分:3)
看起来您要切换文字
$('tr.headrow').click(function() {
// Find the span inside the headrow and toggle its value to '+'
$(this).find('span').text(function(i, t) {
return t.trim() == '-' ? '+' : '-';
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr class="headrow">
<th colspan="3"><span id="bigspan">-</span> Main Header</th>
</tr>
<tr class="headrow">
<th colspan="3"><span id="bigspan1">-</span> Main Header</th>
</tr>
<tr class="headrow">
<th colspan="3"><span id="bigspan2">-</span> Main Header</th>
</tr>
</table>
&#13;
答案 1 :(得分:2)
您可以使用html
的功能根据内容修改内容:
JSFiddle:http://jsfiddle.net/TrueBlueAussie/vwtmwz0a/2/
$('tr.headrow').click(function () {
// Find the span inside the headrow and toggle its value to '+'
$(this).find('span').html(function(){
return $.trim($(this).html()) == '-' ? '+' : '-';
});
});
在这个例子中, text()
同样出色。我习惯使用html()
:)