jQuery:如何处理span的更改文本 [英] jQuery : How to handle change text of span

查看:663
本文介绍了jQuery:如何处理span的更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery,我想在一个范围内显示一些计算(称为 span1 ),我希望在 span1的文本时已更改对其值进行一些计算并显示在其他跨度中(称为span2,span3,...)。我如何处理跨度的文本更改?

I'm using jQuery and I want to show some calcution in a span (called span1) and I want when text of span1 changed do some calcution on it's value and show in other spans(called `span2 ,span3,...). How I can handle text change of span?

谢谢

推荐答案

您可以使用更改span1文本的函数来更改其他文本。

You could use the function that changes the text of span1 to change the text of the others.

作为一种解决方法,如果您真的希望它具有更改事件,然后不要将文本设置为跨度1.而是在jQuery中设置输入变量,向其中写入更改事件,并且更改span1的文本时改为更改输入变量的值,因此触发更改事件,如下所示:

As a work around, if you really want it to have a change event, then don't asign text to span 1. Instead asign an input variable in jQuery, write a change event to it, and whever ur changing the text of span1 .. instead change the value of your input variable, thus firing change event, like so:

var spanChange = $("<input />");
function someFuncToCalculateAndSetTextForSpan1() {
    //  do work
    spanChange.val($newText).change();
};

$(function() {
    spanChange.change(function(e) {
        var $val = $(this).val(),
            $newVal = some*calc-$val;
        $("#span1").text($val);
        $("#spanWhatever").text($newVal);
    });
});

虽然我真的觉得这是解决方法,但在创建简单更改的某些方面很有用事件,非常过度,你最好在改变span1的同时对其他跨度进行更改。

Though I really feel this "work-around", while useful in some aspects of creating a simple change event, is very overextended, and you'd best be making the changes to other spans at the same time you change span1.

这篇关于jQuery:如何处理span的更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆