admin管理员组文章数量:1278948
I have tried different jQuery approaches:
var num = $(this).attr('colspan').text();
var num = $(this).attr('colspan').val();
var num = $(this).('td[colspan]').val();
var num = $(this).('td[colspan]').text();
var num = $(this).('td[colspan]').attr('id');
and this from W3Schools: var num = document.getElementById($(this)).colSpan;
no luck. It's either undefined or an uncaught TypeError in my console.log()
EDIT: I have tried your suggestions and it still isn't working. Here is part of the code in context:
$('table td').on('mouseenter mouseleave', function(e){
var th, index, id, td;
if(e.type === 'mouseenter'){
id = $(this).attr('id');
td = $(this);
var num = $(this).attr('colspan');
console.log("td = "+td+" colspan = "+num);
td.addClass('highlight');
var $table_body_scroll=$('table.body_scroll'),
header_table=$( '<table aria-hidden="true" class="header_table"><thead><tr><td></td></tr></thead></table>' ),
scroll_div='<div class="body_scroll"></div>';
var $targetHeaderTable=$("table.header_table").eq(index);
$('thead tr th').each(function() {
console.log("th = "+$(this).text());
console.log("id = "+id);
if ($(this).text() == id)
{
var num = td.attr('colspan');
//console.log("td = "+td+" colspan = "+num);
$(this).addClass('highlight');
}
});
index = $('td').index($(e.target));
$(e.target).prevAll().addClass('highlight');
}else{
$('.highlight').removeClass('highlight');
}
});
and my console log: td = [object Object] colspan = undefined
I have tried different jQuery approaches:
var num = $(this).attr('colspan').text();
var num = $(this).attr('colspan').val();
var num = $(this).('td[colspan]').val();
var num = $(this).('td[colspan]').text();
var num = $(this).('td[colspan]').attr('id');
and this from W3Schools: var num = document.getElementById($(this)).colSpan;
no luck. It's either undefined or an uncaught TypeError in my console.log()
EDIT: I have tried your suggestions and it still isn't working. Here is part of the code in context:
$('table td').on('mouseenter mouseleave', function(e){
var th, index, id, td;
if(e.type === 'mouseenter'){
id = $(this).attr('id');
td = $(this);
var num = $(this).attr('colspan');
console.log("td = "+td+" colspan = "+num);
td.addClass('highlight');
var $table_body_scroll=$('table.body_scroll'),
header_table=$( '<table aria-hidden="true" class="header_table"><thead><tr><td></td></tr></thead></table>' ),
scroll_div='<div class="body_scroll"></div>';
var $targetHeaderTable=$("table.header_table").eq(index);
$('thead tr th').each(function() {
console.log("th = "+$(this).text());
console.log("id = "+id);
if ($(this).text() == id)
{
var num = td.attr('colspan');
//console.log("td = "+td+" colspan = "+num);
$(this).addClass('highlight');
}
});
index = $('td').index($(e.target));
$(e.target).prevAll().addClass('highlight');
}else{
$('.highlight').removeClass('highlight');
}
});
and my console log: td = [object Object] colspan = undefined
-
Try this:
var num = $(this).attr('colspan')
– MysticMagicϡ Commented Feb 8, 2014 at 16:48 - jsfiddle/A283U – Eich Commented Feb 8, 2014 at 16:50
2 Answers
Reset to default 8Use prop
to get the value set for the attribute/property, this will reflect any changes made to the property with JS after the page load:
$(this).prop("colSpan");
JS Fiddle: http://jsfiddle/9u8L2/1
if this
refers to the td element then
var span = $(this).prop('colSpan')//or
var span = $(this).attr('colspan')
本文标签: javascriptHow to get the value of colspanStack Overflow
版权声明:本文标题:javascript - How to get the value of colspan - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741279483a2369923.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论