繁体   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