我有点困惑为什么这段代码不起作用!
HTML 标记:
<div id="diva"><b>Score</b> some <i>goals</i></div>
<div id="soda"></div>
JavaScript 代码:
function GetSelectedText () {
if (window.getSelection) { // all browsers, except IE before version 9
var range = window.getSelection ();
alert (range.toString ());
}
else {
if (document.selection.createRange) { // Internet Explorer
var range = document.selection.createRange ();
alert (range.text);
}
}
}
var butn = document.getElementById("soda");
butn.onclick = function(){
GetSelectedText();
}