如何用jquery获取html表td属性唯一id

时间:2012-06-13 04:16:22

标签: php jquery mysql html show-hide

大家好我正在尝试为每个HTML table cell创建工具提示你们有什么想法我怎么能在这里获得td属性ID

非常感谢 jquery代码示例

   $(function () {
    $(".test").hover(
        function () {

                        var toolTipHtml = $("#ToolTipDiv").clone();

            $(this).append(toolTipHtml);
            toolTipHtml.show("slow");
        },
        function () {
            var toolTipHtml = $(this).find(".tooltip");

            toolTipHtml.hide();
            toolTipHtml.remove();
        }
    );

});

 echo "<table>";
while($row = mysql_fetch_array($result))
{
      $id = $row['id_out_org'];
           echo "<tr>";  
         echo "<td>" .$row['KG']."</td>";        
        echo "<td class='test'>" .$row['B']."</td>";
        echo "<td >" .$row['B']."</td>";
        echo  "<td class='test'>"; 
        echo "<div id = 'ToolTipDiv' class='tooltip' style='background-color: White; display: none; width: 20%;'>";

        echo "Total: $ "; echo $totalp = $total + $fuel;

        echo "</div>";
         "</td>";


        echo "</tr>";           
    }
    echo "</table>";

4 个答案:

答案 0 :(得分:1)

使用catch-all选择器,然后迭代它们:

$("td[id$=ToolTipDiv_]").each(function() {
    $(this).attr('title', 'some tool tip');
});

答案 1 :(得分:1)

最好使用这个开源插件 Tooltip

它可能满足您的需求。 请参阅示例here

$('#table td.test').tooltip();

答案 2 :(得分:0)

从每个表格单元格中获取HTML和ID:

$('td').each(function() {
  var toolTipHtml = $(this).html(); // now you have the contents of the cell
  id = $(this).attr('id');  // now you have the id
});

答案 3 :(得分:0)

因为您使用的是胡佛,所以您可以使用类似的东西

$('.tooltip').hover(function() {
   alert(this.id);
   // or to get the id of the row
var toolTipHtml = $("#"+this.id).clone();
});