在jQuery中获取动态id



目标是动态检索jquery代码中的id,以便单击href为表中具有相同值的项目着色下面的代码仅使用一个id完成。在smarty中,链接是通过foreach循环动态生成的。如何改变Js代码来动态检索我的id,并在最后,当你点击链接,它的颜色在低线的一切都是搜索词。Smarty HtML代码:

{foreach from=$translateArray key=key item=value}
{if $key == $k}
<a href="#" classe="inactiveLink" id="id_{$k}" style="color:#000080";>{$value}</a> 
({$val}) |
{/if}
{/foreach}

Jquery(dataTable) with id="id_single">

$(document).ready(function () {
var table = $("#my_table").DataTable({
"rowCallback": function (row, data) {
$('#id_single').on('click', function () {
var value = $("#id_single").text();// How to get id dynamically here
console.log(value);
highLight(value, row);
});
function highLight(value, row) {
$(row).css('color', 'unset');
$(row).css('font-weight', 'unset');
if (data[2] === value) {
$(row).css('background-color', 'lightblue');
$(row).css('font-weight', 'bold');
}
}
}
});
});

您可以使用class来表示

{foreach from=$translateArray key=key item=value}
{if $key == $k}
<a href="#" classe="inactiveLink id_single"  style="color:#000080";>{$value}</a> 
({$val}) |
{/if}
{/foreach}
$(document).ready(function () {

var table = $(#my_table")。数据表({rowCallback":函数(行,数据){

$('.id_single').on('click', function () { // first change
var value = $(this).text();// second change
console.log(value);
highLight(value, row);
});
function highLight(value, row) {
$(row).css('color', 'unset');
$(row).css('font-weight', 'unset');
if (data[2] === value) {
$(row).css('background-color', 'lightblue');
$(row).css('font-weight', 'bold');
}
}
}

});});

最新更新