強調表示されたテキストを読み取るコードがいくつかあります。しかし、JavaScript で関数に変数を割り当てるのに問題があります。期待どおりに関数を正しく呼び出しません。
<html>
<head>
<script type="text/javascript">
function getSelectionText()
{
var text = "";
if (window.getSelection)
{
text = window.getSelection().toString();
}
return text;
}
var txt = getSelectionText(); //<-----This is not working???
</script>
</head>
<body>
<p id="p1">Select some of this text then press the button<br /></p>
<button onclick= document.write(txt) >GetText</button>
</body>
</html>
書き込みパラメーターで関数を使用すると、機能します。
<button onclick= document.write(getSelectionText()) >GetText</button>
関数に変数を代入すると、関数が正しく呼び出されないのはなぜですか?
-スコット