DIV CSS 佈局教程網

jQuery 獲取 tr 並修改其內容的示例代碼
編輯:JQuery特效代碼     

在表格操作中,經常需要遍歷每一行(tr)並根據內容做處理。下面以「當郵箱內容過長時自動截斷並添加省略號」為例進行說明。

jQuery 遍歷處理

$(document).ready(function () {
    $("#Email tr").each(function () {
        var objString = $(this).children("#Emailtd").text();
        if (objString.length > 46) {
            // 超過 46 個字符則截斷並追加省略號
            $(this).children("#Emailtd").text(objString.substring(0, 46) + "...");
        }
    });
});

對應的表格結構

下面是一個典型的由後端循環輸出的表格 tbody,其中郵箱列使用 id="Emailtd" 標記:

<tbody id="Email">
<c:forEach items="${Warns}" var="Warn">
    <tr>
        <td>${...}</td>
        <td>${...}</td>
        <td id="Emailtd">${Warn.mail}</td>
        <td>${...}</td>
    </tr>
</c:forEach>
</tbody>

通過 each() 遍歷每一行,再借助 children() 定位到目標列,就能靈活地讀取並修改表格內容。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved