2016-01-22 56 views
0

我想用JQuery來選擇Tablesorter單元格的內容。jQuery Tablesorter,重點選擇內容

例如 我在輸入文本中應用了方法setSelectionRange。 良好的自動選擇文本。

<input onClick="this.setSelectionRange(0, this.value.length)" value="Sample Text" /> 

我想要一個的tablesorter單元格的內容(DIV)

<div class="" contenteditable="true">0,00</div> 

如何同樣的效果?

回答

0

如果你不使用editable widgetfork of tablesorterdemo

$('div[contenteditable]').on('focus', function() { 
    var cell = this; 
    // select all text in contenteditable 
    // see http://stackoverflow.com/a/6150060/145346 
    var range, selection; 
    if (document.body.createTextRange) { 
    range = document.body.createTextRange(); 
    range.moveToElementText(cell); 
    range.select(); 
    } else if (window.getSelection) { 
    selection = window.getSelection(); 
    range = document.createRange(); 
    range.selectNodeContents(cell); 
    selection.removeAllRanges(); 
    selection.addRange(range); 
    } 
}); 

如果您使用的是可編輯的小部件,然後使用editable_selectAll option


更新:此方法效果以及(demo

也就是說,如果您的瀏覽器支持 - 看caniuse & this demo

$('div[contenteditable]').on('focus', function() { 
    setTimeout(function(){ 
    document.execCommand('selectAll', false, null); 
    }, 1); 
}); 
+0

非常感謝。 我申請了第二個腳本完美的作品。雖然我不明白它爲什麼起作用 TNX –