2016-03-31 9 views
0

ウェブページのテキストを強調表示した後にドロップダウンメニューを開くようにしようとしていますが、ドロップダウンが開かない。また、オブジェクトを文字列に変換したので、ウィンドウをクリックしてもテキストが選択されていないときは開きません。文字列ではなくDIVを作成する必要がありますか?テキストをハイライト表示した後にドロップダウンメニューを開く

HTML:

<ul class="dropdown-menu" aria-labelledby="dropdownMenu2"> 
    <li><a href="#">comment</a></li> 
    <li><a href="#">message</a></li> 
    </ul> 

JS:

function getSelected() { 
    if (window.getSelection) { 
     return window.getSelection(); 
    } 
    else if (document.getSelection) { 
     return document.getSelection(); 
    } 
    else { 
     var selection = document.selection && document.selection.createRange(); 
     if (selection.text) { 
      return selection.text; 
     } 
     return false; 
    } 
    return false; 
} 

var selection = getSelected(); 


function checkObj(){ 
    var a = String(selection); 
    var b = a.length; 
    if (b > 0){ 
    $('.dropdown-menu').dropdown('toggle');//this isn't working 
    } 
} 
    $(window).mouseup(function() { 
    var a = checkObj(selection); 

}); 

UPDATE: 私は示すことgottentheメニューをしたが、私は、テキストをハイライト表示して、テキストボックスイムの外にポインタをドラッグする場合にのみ表示さ私はちょうど1つの単語を強調表示した場合、それは動作しません。また、強調表示したテキストの右にドロップダウンを表示することもできます。

http://liveweave.com/vDCkDp

+0

liveweave added – DRE

+0

https://github.com/dcondrey/highlighterWP – davidcondrey

答えて

0

$('.dropdown-menu').dropdown('toggle');

dropdown機能は、おそらくクリックイベントか何かをチェックするため、機能していません。

$('.dropdown-menu').show();メニューを正しく開いているようですが、メニューを閉じるためにjsコードを手動で記述する必要があります。https://css-tricks.com/dangers-stopping-event-propagation/を参照して、要素をクリックするときの適切な方法を確認してください。

関連する問題