如何处理跨度的更改文本 [英] How to handle change text of span
问题描述
我正在使用jQuery,我想显示一个跨度(称为span1
)中的一些计算,并且我希望当span1
的文本更改时,对其值进行一些计算并显示其他跨度(称为`span2, span3,...).如何处理跨度文本更改?
I'm using jQuery and I want to show some calculation in a span (called span1
) and I want when text of span1
changed do some calculation 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.
作为一种变通方法,如果您确实希望它具有一个change
事件,则不要将文本分配给span1.而是在jQuery中分配一个输入变量,向其写入一个change事件,并在每次更改时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.
这篇关于如何处理跨度的更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!