javascript 根据值向表的每个 td 添加标题属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11950155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:51:28  来源:igfitidea点击:

Add an title attribute to every td of a table based on a value

javascriptjquery

提问by monda

I hava 2 tables

我有 2 张桌子

 <table class="first">
    <thead>
          <th> header1</td>
          <th> header2 </th>
    </thead> 
    <tbody>
           <tr>
                <td>hi</td>
                <td>hello</td>
           </tr>
    </tbody>
 </table>


 <table class="second">
    <thead>
          <th> header1</td>
          <th> header2 </th>
    </thead> 
    <tbody>
           <tr>
                <td>hi</td>
                <td>hello</td>
           </tr>
           <tr>
                <td>hi</td>
                <td>hello</td>
           </tr>
    </tbody>
 </table>

now my jquery Part:

现在我的 jquery 部分:

  var trainingInstanceIds = ${trainingInstance.id};
  alert(trainingInstanceIds) // which prints say 1,9,  

now i want every td of second table to have the title as the value given in trainingInstanceIds

现在我希望第二个表的每个 td 都将标题作为 trainingInstanceIds 中给出的值

basically i want something like this

基本上我想要这样的东西

   <table class="second">
    <thead>
          <th> header1</td>
          <th> header2 </th>
    </thead> 
    <tbody>
           <tr>
                <td title="1">hi</td>
                <td title="9">hello</td>
           </tr>
           <tr>
                <td  title="1">hi</td>
                <td  title="9">hello</td>
           </tr>
    </tbody>
 </table>

the value of trainingInstanceIds and the number of td will change .

trainingInstanceIds 的值和 td 的数量会发生变化。

here i want to add title to an already created table

在这里我想为已经创建的表格添加标题

how to do it?

怎么做?

    $.each(trainingInstanceIds, function(index, value) { 

   });

回答by fredrik

You could just pass a function to jQuery's attr-method:

您可以将一个函数传递给 jQuery 的attr-method:

var trainingInstanceArray = [1, 9]; 
// OR use if it's a string:
var trainingInstanceArray = trainingInstanceIds.split(',')
$('.second td').attr('title', function (index, attr) {
    return trainingInstanceArray[$(this).index()];
});?

Example at JSFiddle

JSFiddle 中的示例

回答by Linus Gustav Larsson Thiel

It might yield better performance to iterate over the tds instead of the ids:

迭代tds 而不是 id可能会产生更好的性能:

var trainingInstanceArray = trainingInstanceIds.split(',');

$('.second td').each(function(index, element) {
  var $el = $(this);
  $el.attr('title', trainingInstanceArray[$el.index()]);
});

Edit:I missed that you wanted every title for every row. Updated above.

编辑:我错过了你想要每一行的每个标题。以上更新。

回答by Charles Jourdan

// I suppose that trainingInstanceIds is a table
$.each( $("table.second tbody tr"), function(){
    var tds = $(this).find("td");
    $.each(trainingInstanceIds, function(index, value) {
       $(tds[index]).attr("title", value);
    });
});

I think something like this. But you assume attribute title is not w3c compliant.

我想这样的事情。但是您假设属性 title 不符合 w3c。

回答by Yograj Gupta

Try this, if trainingInstanceIds is coming as an array.

试试这个,如果 trainingInstanceIds 作为数组出现。

$.each(trainingInstanceIds, function(index, value) {
    $('.second td:eq('+ index+')').prop('title', value);
});

or use if trainingInstanceIds coming as string

或使用如果 trainingInstanceIds 作为字符串

$.each(trainingInstanceIds.split(','), function(index, value) {
    $('.second td:eq('+ index+')').prop('title', value);
});

and Check this DEMO

并检查此演示