简体   繁体   English

如何添加div跨此表行内?

[英]How to add div to span inside this table row?

I have a table row as such: 我有这样的表行:

        <tr class="song-row song-row-selected" data-id="1">
                <td data-col="title">
                    <span class="song-content">
                        <img src="img/cover3.jpg" />
                        Song Title
                    </span>
                </td>
                <td>3:37</td>
                <td>song artist</td>
                <td>song album</td>
                <td>23</td>
            </tr>

I want to add a div to indicate that the song is paused (surrounded by *): 我想添加一个div来指示歌曲已暂停(由*包围):

        <tr class="song-row song-row-selected" data-id="1">
                <td data-col="title">
                    <span class="song-content">
                        <img src="img/cover3.jpg" />
                        Song Title
                        *<div class="song-indicator loading"></div>*
                    </span>
                </td>
                <td>3:37</td>
                <td>song artist</td>
                <td>song album</td>
                <td>23</td>
            </tr>    

I was hoping to use JQuery. 我希望使用JQuery。 So far I have: 到目前为止,我有:

function displayPause() {
    $('tr.song-row.song-row-selected:first').each(function() {
        $(this).siblings('td span.song-content').add('<div class="song-indicator paused"></div>');
    });
}

Needless to say, it doesn't add the div. 不用说,它不会添加div。 Also, I would like a function for me to easily remove the div from the span. 另外,我希望我可以轻松地从范围中删除div。 Does anyone know where to point me in the right direction? 有谁知道向我指出正确方向的地方?

append is what you want: 追加是您想要的:

$(this).find('td span.song-content')
       .append('<div class="song-indicator paused"></div>');

This will ... append the div to the end of your span. 这将... div 附加到范围的末尾。

Also, as tymeJV says, your td is not a sibling of your tr ; 而且,正如tymeJV所说,您的td并不是您tr的兄弟姐妹; it's a child. 这是一个孩子。 Use either children, or find to grab it. 请使用一个孩子,或寻找它。

And to remove it, you'd use remove . 要删除它,可以使用remove If I understand your app correctly, it should be something like this: 如果我正确了解您的应用,则应该是这样的:

$('tr.song-row.song-row-selected:first')
        .find("div.song-indicator.paused").remove(); 

You should be using append as well as children , not siblings 您应该同时使用appendchildren ,而不要使用siblings

$(this).children('td span.song-content').append('<div class="song-indicator paused"></div><input type="button" class="removeDiv" value="Remove"/>');

I also added a button right next to your div, clicking this will remove that div with this code: 我还在您的div旁边添加了一个按钮,单击此按钮将使用以下代码删除该div:

$(document).on("click", ".removeDir", function() {
    $(this).prev(".song-indicator").remove();
    $(this).remove();
});

这是jsfiddle上的一个小样本

$("tr.song-row-selected:first td span.song-content").append("*<div class='song-indicator loading'></div>*");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM