Я хотел бы предложить:
/* binds a click-handler to the 'tbody' element,
filters those clicks to only those that originate on a 'td' element:
*/
$('tbody').on('click', 'td', function(){
/* gets the siblings of the clicked 'td' element, filters those siblings
for 'th' elements, gets the first of those matching elements,
and then retrieves the text of that element, assigning it to the variable:
*/
var thText = $(this).siblings('th').first().text();
console.log(thText);
});
JS Fiddle demo.
Или, используя немного больше DOM (маленькое, крошечного увеличения эффективности):
// same as above:
$('tbody').on('click', 'td', function(){
/* using DOM (not jQuery), moving from the clicked 'td' element
to its parentNode ('tr'), retrieving the first of its children
(JavaScript is zero-indexed, the first child is at position '0'),
and retrieving its textContent (the text of the 'th'):
*/
var thText = this.parentNode.children[0].textContent;
console.log(thText);
});
JS Fiddle demo.
Ссылки:
* SyntaxError: отсутствует) после списка аргументов * –
'предупреждение ($ (это) .closest ('тр') находим ('ю') текст());' HTTP:.//jsfiddle.net/T4tua/ –
Это слишком сложная демонстрация. Вы хотите щелкнуть 'td' (* any *' td??) И извлечь текст из 'th' в начале щелкнутой строки? –