如何获得colspan的价值 [英] How to get the value of colspan

查看:65
本文介绍了如何获得colspan的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过不同的jQuery方法:

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');

这来自W3Schools: var num = document.getElementById($(this ))。colSpan;

and this from W3Schools: var num = document.getElementById($(this)).colSpan;

没有运气。它在我的 console.log()中是未定义的或未被捕获的TypeError。

编辑:
我已经尝试了你的建议但它仍然无效。以下是上下文代码的一部分:

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');
    }
});

和我的控制台日志: td = [object Object] colspan = undefined

推荐答案

使用 prop 获取值为属性/属性设置,这将反映页面加载后使用JS对属性所做的任何更改:

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小提琴: http://jsfiddle.net/9u8L2/1

这篇关于如何获得colspan的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆