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

Share Improve this question edited Feb 8, 2014 at 17:01 Burning Hippo asked Feb 8, 2014 at 16:46 Burning HippoBurning Hippo 8052 gold badges22 silver badges47 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

Reset to default 8

Use 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