簡體   English   中英

jQuery文本函數-如何傳遞所選元素的當前值

[英]jquery text function - how to pass on the current value of the selected element

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文本函數中添加一些內容,通過它可以切換所選元素的文本值(此處為span)。 我基本上想將其更改為“ +”,如果其當前值是“-”,反之亦然

看起來您想切換文字

 $('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() == '-' ? '+' : '-'; }); }); 
 <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> 

您可以將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() :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM