0

テキストを含む div 要素があります。選択できるようにする必要があります: 1. div 自体、および 2. その div 内のテキストのみ

<div class="myDiv">some text</div>

したがって、クラス名でdivを選択できます

$(".myDiv").hover(...);

でも、文章が苦手です。以下を試しました

$(".myDiv").contents().eq(0).text().hover(...);

しかし運がない。また、テキストにカーソルを合わせると、親の div ではなく、そのテキストのみを選択する必要があります。

ここに画像の説明を入力

4

4 に答える 4

1

デモ

これを試して

私はこれがあなたが必要とするものだと思います

(function findTextNodes(current, callback) {
    for(var i = current.childNodes.length; i--;){
        var child = current.childNodes[i];
        if(3 === child.nodeType)
            callback(child);
        findTextNodes(child, callback);
    }
})(document.getElementById('myDiv'), function(textNode){ 
    $(textNode).replaceWith('<span class="textNode">' + textNode.nodeValue + '</span>');
});

$('.textNode').hover(function(){

    $(this).css('color', 'white'); 
},function(){
    $(this).css('color', 'black');
});
于 2013-08-30T16:54:12.263 に答える
0
var html= $('.myDiv').html(); // Select div itself
var text= $('.myDiv').text(); // To get only the text
于 2013-08-30T15:43:26.143 に答える
0

テキストを追加しようとしている場合は、次を試してください。

$(".myDiv").contents().eq(0).innerHTML('YOUR TEXT HERE').hover(...);

于 2013-08-30T15:43:29.767 に答える