JQuery $(this).find(“#id”)。text(“text”)不改变文本

时间:2017-08-10 02:38:09

标签: javascript jquery element

我正在遍历页面上某个类的所有元素,并使用特定的id编辑该类中标记的文本。我使用$(this).find('#time')引用元素并尝试使用$(this).find('#time').text("test")更改该对象的文本,但元素的文本没有改变,我无法弄清楚原因。

编辑: $('.box').each(function(i, obj){}这就是循环,奇怪的是,当我简单地用$(this).find('#time').text()引用文本时,我会收到正确的输出。但使用.text()时,文字不会改变。

以下是用于更改对象文本的代码:

var time = response.substring(7, 15);
var user = response.substring(16, response.length);
$(this).find('#time').text(time);
$(this).find('#name').text(response.substring(user));

游戏页面

<div class="box">
    <h1>&#36;{{ game_object.amount }}</h1>
    <h2 id="time">{{ game_object.start_time}}</h2>
    <p id="name">{{ game_object.current_top_user }}</p>
    <a href="{% url 'game_page' game.pk %}" class="click_play">CLICK NOW</a>
</div>

1 个答案:

答案 0 :(得分:-4)

尝试使用.html

$(this).find('#time').html("test")

或者这可以帮到你:

$('body').find('#time').text('test');

因为元素的ID必须是唯一的