开发者

How to add tooltip by matching the IDs in HTML using Jquery

开发者 https://www.devze.com 2022-12-20 18:22 出处:网络
I am using HTML and Jquery I have got below issue, I have an below HTML code <table style=\"border-top-style: solid; border-right-style: solid; border-left-style: solid;

I am using HTML and Jquery

I have got below issue, I have an below HTML code

<table style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
    border-bottom-style: solid" cellspacing="1" cellpadding="1" width="100%" border="1">
    <tbody>
        <tr>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                background-color: #dfdfdf; text-align: left; border-bottom-style: solid" align="left">
                <strong>Type of Course</strong>
            </td>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                background-color: #dfdfdf; text-align: left; border-bottom-style: solid" align="left">
                <strong>Courses </strong>
            </td>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                background-color: #dfdfdf; text-align: left; border-bottom-style: solid" align="left">
                <strong>Credits</strong>
            </td>
        </tr>
        <tr>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                background-color: #dfdfdf; border-bottom-style: solid">
                Core
            </td>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                border-bottom-style: solid" id="Communication">
                Communication Course
            </td>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                border-bottom-style: solid">
                5
            </td>
        </tr>
        <tr>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                border-bottom-style: solid">
            </td>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                border-bottom-style: solid">
                English Course
            </td>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                border-bottom-style: solid">
                5
            </td>
        </tr>
        <tr>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                border-bottom-style: solid">
            </td>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                border-bottom-style: solid" id="Mathematics">
                Mathematics Course
            </td>
            <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
                border-bottom-style: solid">
                5
            </td>
        </tr>
    </tbody>
</table>

<div style="display: none">
    <ul>
        <li id="CommunicationTooltip">
            <a href="#" class="toolTip">
                <img src="/images/q_mark.gif" alt=""/><span style="width: 500px;">Testing Communication.</span>
            </a>
        </li>
        <li id="MathematicsTooltip">
            <a href="#" class="toolTip">
                <img src="/images/q_mark.gif" alt=""/><span style="width: 500px;">Testing Mathematics.</span>
            </a>
     开发者_Go百科   </li>
    </ul>
</div>  

In above HTML code you will see that we have many TD, some without there ID and some TD are having ID and below you see that I have DIV with LI having ID same as used in above TD with extra Tooltip in it.

Now I want when pages loaded matching there ID (TD and LI) including extra Tooltip text, the below A HREF text will be added in that particular TD,so that it will result below HTML for that TD

   <td style="border-top-style: solid; border-right-style: solid; border-left-style: solid;
        border-bottom-style: solid" id="Communication">
        Communication Course 
<a href="#" class="toolTip">
        <img src="/images/q_mark.gif" alt=""/><span style="width: 500px;">Testing Communication.</span>
    </a>
    </td>

Please suggest solution using Jquery


Based on your markup, this should do it:

$(document).ready(function() {

    // bind to cells with an ID attribute
    $("table > tbody > tr > td[id]").mouseover(function() {

        // grab the anchor from the LI whose ID starts with the cell's ID
        var $tooltip = $("div:hidden li[id^=" + $(this).attr("id") + "] a");

        // append it to the current cell
        $(this).append($tooltip);
    }).mouseout(function() {

        // remove the anchor/tooltip
        $(this).find("a").remove();
    });
});
0

精彩评论

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