开发者

Get opening tag with jquery

I have an issue where I need to format html that's stored in a client cache in the following format:

html[0] = '<tr>'
html[1] = '<td>text</td>'
html[2] = '<td>text</td>'

On normal occasions I work directly with the dom and don't need to go near this cache but sometimes I need to update it like so...

html[1] = '<td&开发者_StackOverflowgt;<a href="">text</a></td>'

Because of the 'unusual' storage of the opening 'tr' am I able somehow to construct a new 'tr', manipulate it, then render only the opening tag?

$(html[0] + '</tr>');

I'm aware this could be done with regexp but curious to know if there's a jquery technique I can use.


HTML is not XML. In HTML this is perfectly legal:

<table>
    <tr>
        <td>Text</td>
        <td>Text</td>
    <tr>
        <td>Text</td>
        <td>Text</td>
</table>

That means that you don't have to close the opening <tr> bracket with </tr>.

You can use the W3C Markup Validation Service to check your HTML for validness.

You can wrap the text with an anchor like this:

<!doctype html>
<html>
<head>
    <title>Wrap td text</title>
    <script type="text/javascript" 
        src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var html = [];
            html[0] = '<tr>';
            html[1] = '<td>text</td>';
            html[2] = '<td>text</td>';
            $(html.join(""))
                .children("td")
                    .each(function () {
                        $(this).html($('<a href="">' + $(this).text() + '</a>'));
                    })
                    .end()
                .appendTo("table");
        });
    </script>
</head>
<body>
    <table>
    </table>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜