有没有办法确定所选文本的样式

时间:2015-07-26 16:48:57

标签: javascript jquery

使用此代码段:

using namespace std;

int game();
string playAgain();

int main(){

game();
playAgain();

return 0;
}

int game(){

int guess;
int guessesTaken = 0;
int number = rand() % 10 + 1;
int count = 0;

cout << "I am thinking of a number between 1 and 10, can you guess it? " << endl;

while (guessesTaken < count){
    cout << "Take a guess: " << endl;
    cin >> guess;
    if (guess > number)
        cout << "Too High. Try again!" << endl;
    if (guess < number)
        cout << "Too Low! Try again!" << endl;
    if (guess == number)
        break;

}count++;

if (guess == number)
    cout << "Congratulations!" << endl;
return 0;
}

string playAgain(){
string play;

cout << "Want to play again?: " << endl;
if (play == "y" || "Y")
    main();
else
    cout << "Thanks for playin" << endl;
return 0;

现在,很明显这段代码将从大型自由文本浮动中返回选定的文本。我很好奇的是,您如何确定所选文本是否正常,粗体斜体或下划线。

修改 https://developer.mozilla.org/en-US/docs/Web/API/Selection/focusNode

function GetSelectedText()
{
  var selectedText=(
        window.getSelection
        ?
            window.getSelection()
        :
            document.getSelection
            ?
                document.getSelection()
            :
                document.selection.createRange().text
     );
 if(!selectedText || selectedText=="")
 {
    if(document.activeElement.selectionStart)
    {
     selectedText = document.activeElement.value.substring(
          document.activeElement.selectionStart
          . document.activeElement.selectionEnd);
    }
 }
 return selectedText;
}

0 个答案:

没有答案