简体   繁体   中英

javascript setting attributes in a for loop

I've been developing an app and I've gotten the formatting to work using the following javascript:

$('td:eq(1)', nRow).attr('title', full_text);
$('td:eq(1)', nRow).css('cursor', 'pointer');
$('td:eq(2)', nRow).attr('title', full_text);
$('td:eq(2)', nRow).css('cursor', 'pointer');
$('td:eq(3)', nRow).attr('title', full_text);
$('td:eq(3)', nRow).css('cursor', 'pointer');
$('td:eq(4)', nRow).attr('title', full_text);
$('td:eq(4)', nRow).css('cursor', 'pointer');
$('td:eq(5)', nRow).attr('title', full_text);
$('td:eq(5)', nRow).css('cursor', 'pointer');
$('td:eq(6)', nRow).attr('title', full_text);
$('td:eq(6)', nRow).css('cursor', 'pointer');
$('td:eq(7)', nRow).attr('title', full_text);
$('td:eq(7)', nRow).css('cursor', 'pointer');
$('td:eq(8)', nRow).attr('title', full_text);
$('td:eq(8)', nRow).css('cursor', 'pointer');
$('td:eq(9)', nRow).attr('title', full_text);
$('td:eq(9)', nRow).css('cursor', 'pointer');
$('td:eq(10)', nRow).attr('title', full_text);
$('td:eq(10)', nRow).css('cursor', 'pointer');
$('td:eq(11)', nRow).attr('title', full_text);
$('td:eq(11)', nRow).css('cursor', 'pointer');
$('td:eq(12)', nRow).attr('title', full_text);
$('td:eq(12)', nRow).css('cursor', 'pointer');

I actually build this in R as I'm not an expert in javascript. The entire table to which this applies has a total of 250 columns so I generate a very large string and then use that in my app. How would I do it directly if the datatable has 250 columns?

Apologies if it's a simple question, I'm more of a statistician...

Here is full_text:

var full_text = 'ID: ' + aData[2] + '\\n' + 'NAME: ' +aData[4] + '\\n' + 'CRNCY: ' +aData[5] + '\\n' + 'Type: ' +aData[6] + '\\n' + 'Type2: ' +aData[7] + '\\n' + 'Sector: ' +aData[8] + '\\n' + 'Industry: ' +aData[9] + '\\n' + 'Security: ' +aData[10] + '\\n' + 'Stype2: ' +aData[11] + '\\n';

aData is a data.table that is made by my RShiny app.

Try this one

    var full_text = 'ID: ' + aData[2] + '\\n' + 'NAME: ' +aData[4] + '\\n' + 'CRNCY: ' +aData[5] + '\\n' + 'Type: ' +aData[6] + '\\n' + 'Type2: ' +aData[7] + '\\n' + 'Sector: ' +aData[8] + '\\n' + 'Industry: ' +aData[9] + '\\n' + 'Security: ' +aData[10] + '\\n' + 'Stype2: ' +aData[11] + '\\n';
    $('td').each(function(){
        $(this).css('cursor', 'pointer');
        $(this).attr('title', full_text);
    });

or, probably, $('td', nRow) will be better then $('td') .

I work with jquery around 3 or 4 years ago, and I forgot couple of details. When I look into your code, I can see $('td:eq(3)', nRow). Most probably, second argument display context, where search should be applied. So,

   $('td')//will find all 'td' elements on page`
   $('td', nRow)//will find all td inside of nRow element

If you will have two or more tables on your webpage, search by $('td') will bring elements that should not appear inside of your script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM