簡體   English   中英

將SPAN值重置為默認值

[英]Reset SPAN value to Default

我有以下HTML代碼,想將SPAN值重置為0。Textarea和textbox都可以正常工作。

的HTML

<div id="Match">FULL MATCH: <span class="fullMatch">0</span></div>
<input type="text" id="c-match" value="" />

JS

jQuery('.c-reset').on('click', function() {
    jQuery('span').text(''); //this only removes the values without reverting back to 0
    jQuery('textarea, #c-match').val(function() {
        return this.defaultValue;
    });
});

任何幫助都感激不盡。 謝謝

嘗試html()而不是text()

jQuery('span').html('0');

更新: span沒有默認值,您必須手動設置其內容。

jQuery('span').text('0'); 也會工作

有幾種方法可以做到這一點,我將建議兩種我個人認為最好的方法:

將默認值存儲在變量中

jQuery.ready(function(){//ready function
    var spanDefault = jQuery('#Match span').text();
    jQuery('.c-reset').on('click', function() {
        jQuery('textarea, #c-match').val(function() {
            setTimetou(function(){
                jQuery('#Match span').text(spanDefault);
            }, 1);
            return this.defaultValue;
        });
    });
});

使用數據屬性

<span>設置屬性data-value="0" ,請參閱:

<div id="Match">FULL MATCH: <span data-value="0" class="fullMatch">0</span></div>
<input type="text" id="c-match" value="" />

JS:

jQuery('.c-reset').on('click', function() {
    var fullMatch = jQuery('#c-match span');
    fullMatch.text(fullMatch.data("value"));

    jQuery('textarea, #c-match').val(function() {
        return this.defaultValue;
    });
});

暫無
暫無

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

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