【问题标题】:How to select a td from a table with id using jQuery如何使用 jQuery 从带有 id 的表中选择一个 td
【发布时间】:2014-03-10 08:05:03
【问题描述】:

我有一张带有 td 和 id 的桌子。我需要选择那些 td 并重新排序列。

$('table tr').each(function () {
    var tr = $(this);
    var tds = $('#Status');
    var tdA = $('#Address');
    alert(tds.innerHtml); //Here am getting a blank msg
    tds.remove().insertAfter(tda); //This is what i need to do 
});

【问题讨论】:

标签: javascript jquery


【解决方案1】:

我找到了答案:

var tds = tr.find("td[id='Status']"); //我在找什么

感谢您的支持,特别感谢您对我的真正问题投了 2 分以下的投票:D,因为我不是很饿,所以没有冒犯 :-)

【讨论】:

    【解决方案2】:
    var selectedTd = $("#ID_OF_TD");
    

    或者调用方法比如点击等,你可以直接调用方法

    $("#ID_OF_TD").click(function(){
    
    });
    

    您需要将此代码放入文档准备部分..

    $("document").ready(function(){             
            $("#ID_OF_TD").click(function(){
                alert('td clicked');
            });     
    });
    

    【讨论】:

    • 嘿,但这将返回特定的 td,如果它对你有用,那就太好了.. 但现在我可以看到你已经改变了问题,现在你说你想重新排序列,是是这样吗?然后你必须做很多其他代码。我希望你不能这样做。否则让我知道..
    【解决方案3】:

    只需$("#idHere")

    更多信息:http://api.jquery.com/category/selectors/

    【讨论】:

    • 我之前很伤心,id 很容易被复制,但是当然,最好使用类名,而不是 id。
    • 这就是你否决我的答案的原因吗?认真的吗?
    • @FSou1 ID 不能重复。 它在每一个 HTML 规范中都有规定。document.getElementById('id') 只返回一个元素是有原因的。 $('#id') 只返回一个元素是有原因的。
    • @FSou1 如需官方链接,请查阅 HTML5 生活标准正文。开发者版本提供了一个易于阅读的版本:developers.whatwg.org/content-models.html#the-id-attribute 注意上面写着:The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.
    • @FSou1 你在自欺欺人,请仔细阅读弗洛里安的评论,停止与全世界争论,包括 HTML 规范
    【解决方案4】:

    使用Id Selector

    $('#tdID')
    //^# followed by html id
    

    【讨论】:

      【解决方案5】:

      使用 jQuery 申请:$('#td_ID')

      【讨论】:

        【解决方案6】:

        我们可以使用:

        $('table > tr > td#Status');
        $('table > tr > td#Address');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-16
          • 1970-01-01
          • 2015-08-02
          • 2016-07-29
          • 2011-04-09
          • 2011-04-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多