Maison > Questions et réponses > le corps du texte
version jquery : 1.8
Code original
<tr>
<td>1</td>
<td>11,12</td>
</tr>
<tr>
<td>2</td>
<td>21,22</td>
</tr>
changé en
<tr>
<td>1</td>
<td><a>11</a><a>12</a></td>
</tr>
<tr>
<td>2</td>
<td><a>21</a><a>22</a></td>
</tr>
J'ai essayé d'ajouter id="img" à <td>11,12</td>
, mais $("#img").eq(1).html() ne peut pas obtenir le contenu de la deuxième ligne.
for(var i=0; $("#img").length; i++){
//$("#img").eq(i).html需要非空判断
var arr= $("#img").eq(i).html.spilt();
var str;
for(var j=0; aar.length; j++){
str = "<a>"+arr[j]+"</a>";
}
$("#img").eq(i).html(str);
}
Je ne connais pas très bien jQuery. J'espère que les experts pourront aider à corriger certaines erreurs grammaticales, ce qui cause des problèmes aux experts.
阿神2017-07-05 10:59:16
Je ne sais pas si vous êtes plus clair, l'identifiant est unique, et il ne peut y avoir qu'un seul même nom d'identifiant dans une page de codes html ! Donc $("#img").eq(1)
Un tel code n'est certainement pas conforme à la spécification.
Vérifiez visuellement votre code et modifiez-le de l'ajout d'un identifiant à l'ajout d'une classe. De plus, vous pouvez utiliser la méthode each de jquery pour parcourir les nœuds dom :
var $img = $('.img');'
$img.each(function(index,item){
var aar = $(item).text().split(','),
str = '';
arr.forEach(function(item1,index1){
str = "<a>"+item1+"</a>";
});
$(item).html(str);
});
滿天的星座2017-07-05 10:59:16
$('tr').each(function(){
var _arr = $(this).children("td:eq(1)").html().split(",");
var _str = "";
for(var i in _arr){
_str +="<a>"+_arr[i]+"</a>"
}
$(this).children("td:eq(1)").html(_str);
})
怪我咯2017-07-05 10:59:16
id ne peut être qu'unique et ne peut obtenir que la première valeur, alors utilisez class
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
<body>
<table>
<tr>
<td>1</td>
<td>11,12</td>
</tr>
<tr>
<td>2</td>
<td>21,22</td>
</tr>
</table>
</body>
<script type="text/javascript">
$(function () {
$('tr').each(function(){
$(this).addClass("img"); // 这里加入class
var dom = $(this).find("td").eq(1);
dom.each(function () {
var arr = $(this).html().split(",");
var len = arr.length;
var str = '';
for (let i = 0; i < len; i++) {
str += '<a>' + arr[i] + "</a>"
}
console.log($(this), len, str);
$(this).html(str);
});
});
// 这里你可以输出来看看
console.log($(".img").eq(1).html())
})
</script>
</html>