Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改元素';具有变量id的文本_Javascript_Html_Ajax_Thymeleaf - Fatal编程技术网

Javascript 更改元素';具有变量id的文本

Javascript 更改元素';具有变量id的文本,javascript,html,ajax,thymeleaf,Javascript,Html,Ajax,Thymeleaf,我正在设计一个有时间线的社交网络,还有一个类似按钮。我使用AJAX在服务器端应用like按钮。问题是,我想在每个帖子成功喜欢之后立即更改其喜欢的数量。因为我的元素是由for each生成的,所以我想更改确切元素的like数,我确实有一个问题,我使用的是thymeleaf 我正在寻找一个如何做到这一点的想法 以下是我的html代码: <div class="col-sm-4"> <div class="row" > <div class="col-s

我正在设计一个有时间线的社交网络,还有一个类似按钮。我使用AJAX在服务器端应用like按钮。问题是,我想在每个帖子成功喜欢之后立即更改其喜欢的数量。因为我的元素是由for each生成的,所以我想更改确切元素的like数,我确实有一个问题,我使用的是thymeleaf

我正在寻找一个如何做到这一点的想法

以下是我的html代码:

<div class="col-sm-4">
   <div class="row" >
      <div class="col-sm-12">
      <img th:if="${tweet.isFavorited()}" src="../static/images/like.png" th:src="@{/images/like.png}" th:class="like-img" th:id="${tweet.getId()}"  width="35" height="35"/>
      <img th:if="${!tweet.isFavorited()}"  src="../static/images/dislike.png" th:src="@{/images/dislike.png}" th:class="like-img"  th:id="${tweet.getId()}"   width="35" height="35"/>
   </div>
</div>
<div class="row">
   <div class="col-sm-12" >
     <h6 th:if="${tweet.isRetweet()}" th:class="like-count" th:id="${tweet.getId()}"  th:text="${tweet.getRetweetedStatus().getFavoriteCount()}"></h6>
     <h6 th:if="${!tweet.isRetweet()}" th:class="like-count"  th:id="${tweet.getId()}"  th:text="${tweet.getFavoriteCount()}"></h6>
   </div>
</div>

好的,为了实现这一点,您需要为like count元素分配唯一的ID,如下所示:

<h6 th:if="${tweet.isRetweet()}" th:class="like-count" th:id="${tweet.getId()}_like_count"  th:text="${tweet.getRetweetedStatus().getFavoriteCount()}"></h6>
var currentCount = parseInt($('#'+toSend.tweetId+'_like_count').innerHtml)
var newCount = currentCount++;
$('#'+toSend.tweetId+'_like_count').text(newCount);

好的,为了实现这一点,您需要为like count元素分配唯一的ID,如下所示:

<h6 th:if="${tweet.isRetweet()}" th:class="like-count" th:id="${tweet.getId()}_like_count"  th:text="${tweet.getRetweetedStatus().getFavoriteCount()}"></h6>
var currentCount = parseInt($('#'+toSend.tweetId+'_like_count').innerHtml)
var newCount = currentCount++;
$('#'+toSend.tweetId+'_like_count').text(newCount);
实际上是$('#'+toSend.tweetId+''u like_count')。innerHtml返回undifend!使用.text()而不是.innerHtml。要设置文本,请使用元素.text(“新文本”);实际上是$('#'+toSend.tweetId+''u like_count')。innerHtml返回undifend!使用.text()而不是.innerHtml。要设置文本,请使用元素.text(“新文本”);